「java拼接文件」java音频拼接
今天给各位分享java拼接文件的知识,其中也会对java音频拼接进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何使用java合并多个文件
- 2、怎么把若干文件拼接到一起 java 250分
- 3、如何用Java实现两个文件的拼接
- 4、java怎么把两个文件合成一个文件
- 5、java中如何将两个文件合并到另一个文件
如何使用java合并多个文件
使用java编程语言,对文件进行操作,合并多个文件,代码如下:
import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
public class test {
public static final int BUFSIZE = 1024 * 8;
public static void mergeFiles(String outFile, String[] files) {
FileChannel outChannel = null;
out.println("Merge " + Arrays.toString(files) + " into " + outFile);
try {
outChannel = new FileOutputStream(outFile).getChannel();
for(String f : files){
FileChannel fc = new FileInputStream(f).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb) != -1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!! ");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}
}
}
//下面代码是将D盘的1.txt 2.txt 3.txt文件合并成out.txt文件。
public static void main(String[] args) {
mergeFiles("D:/output.txt", new String[]{"D:/1.txt", "D:/2.txt", "D:/3.txt"});
}
}
怎么把若干文件拼接到一起 java 250分
使用io流把首先读取第一个文件 保存到要保存的位置 然后再打开第二个文件读取 再继续保存 全部保存结束 关闭io流
如何用Java实现两个文件的拼接
如何用Java实现两个文件的拼接
String类的方法:
①利用运算符"+"
②public String concat(String str)进行字符串的拼接操作
StringBuffer的方法:
①public StringBuffer append(String str)将str添加到当前字符串缓冲区的字符序列的末尾
②public StringBuffer insert(int offset,String str)在当前字符串缓冲区的字符序列的下标
索引offset插入str。如果offset等于旧长度,则str添加在字符串缓冲区的尾部
java怎么把两个文件合成一个文件
import java.io.*;
public class zuoye2 {
public static void main(String args[])throws IOException{
FileReader fr1=new FileReader("d:\\wen1.txt");
FileReader fr2=new FileReader("d:\\wen2.txt");
BufferedReader br1=new BufferedReader(fr1);
BufferedReader br2=new BufferedReader(fr2);
BufferedWriter bw1=new BufferedWriter(new FileWriter("D:/wen3.txt"));
int lineNum=0;
String s,s1,s2;
System.out.println("输入文件是:c:\\wenjian12.txt\n输出文件是:c:\\wenjian3.txt");
s1=br1.readLine();
s2=br2.readLine();
while(s1!=null) {
lineNum++;
bw1.write(String.valueOf(lineNum));
bw1.write(": ");
bw1.write(s1);
bw1.newLine();
s1=br1.readLine();
}
while(s2!=null) {
lineNum++;
bw1.write(String.valueOf(lineNum));
bw1.write(": ");
bw1.write(s2);
bw1.newLine();
s2=br2.readLine();
}
bw1.close();
}
}
共两处错误:
第一个是在while里面,没有跳出循环,while的条件并没有发生变化;
第二处就是第一次while完之后:就把bw1关闭掉了,然后第二个while里面还想用bw1;所以只需在末尾关闭即可!
java中如何将两个文件合并到另一个文件
java可以使用FileChannel快速高效地将多个文件合并到一起,以下是详细代码:
import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
public class test {
public static final int BUFSIZE = 1024 * 8;
public static void mergeFiles(String outFile, String[] files) {
FileChannel outChannel = null;
out.println("Merge " + Arrays.toString(files) + " into " + outFile);
try {
outChannel = new FileOutputStream(outFile).getChannel();
for(String f : files){
FileChannel fc = new FileInputStream(f).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb) != -1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!! ");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}
}
}
public static void main(String[] args) {
mergeFiles("D:/output.txt", new String[]{"D:/in_1.txt", "D:/in_2.txt", "D:/in_3.txt"});
}
}
关于java拼接文件和java音频拼接的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。