如何用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合并多个文件
使用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 怎么合并两个wav文件?
//帮你写了一个,是两个mp3文件的合并
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 把两个.mp3文件合并成一个.mp3文件
*
* @author wangran
*
*/
public class Merger {
public Merger() {
}
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//源文件
File in1 = new File("D:/杂/娱乐/音乐/hero.mp3");
File in2 = new File("D:/杂/娱乐/音乐/careless whisper.mp3");
//目标文件
File out = new File("D:/music2.mp3");
//进行流操作
try {
fis = new FileInputStream(in1);
fos = new FileOutputStream(out, true);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] buf = new byte[1024];
while ((len = bis.read(buf))!=-1) {
bos.write(buf,0,len);
}
bos.flush();
fis = new FileInputStream(in2);
bis = new BufferedInputStream(fis);
while ((len = bis.read(buf)) != -1) {
bos.write(buf,0,len);
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java合并两个txt文件并生成新txt
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
/**
* 2015年11月18日上午9:31:05
*
* @author cs2110 TODO 合并数组
*
*/
public class MergeFile {
private String afile = "D:/1.txt";
private String bfile = "D:/2.txt";
private String mergefile = "D:/3.txt";
/**
* 读取文件里面的整数
*
* @param input
* Scanner对象
* @return 返回整形数组
*/
public int[] readFile(Scanner input) {
try {
String temp = "";
while (input.hasNextInt()) {
temp += input.nextInt() + ",";
}
String[] nums = temp.split(",");
int[] arr = new int[nums.length];
for (int index = 0; index nums.length; index++) {
arr[index] = Integer.parseInt(nums[index]);
}
return arr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 合并数组
*
* @param a
* 数组1
* @param b
* 数组2
* @return 整形数组
*/
public int[] merge(int[] a, int[] b) {
int len = a.length;
if (b.length len) {
len = b.length;
}
int[] all = new int[a.length + b.length];
int index = 0;
int aIndex = 0;
int bIndex = 0;
while (aIndex len || bIndex len) {
if (a[aIndex] b[bIndex]) {
all[index] = a[aIndex];
aIndex++;
} else {
all[index] = b[bIndex];
bIndex++;
}
index++;
}
if (aIndex a.length) {
while (aIndex a.length) {
all[index++] = a[aIndex++];
}
} else {
while (bIndex b.length) {
all[index++] = b[bIndex++];
}
}
return all;
}
/**
* 写入文件
*
* @param print
* PrintStream
* @param a
* 数组
*/
public void writeFile(PrintStream print, int[] a) {
for (int index = 0; null != a index a.length; index++) {
print.append(a[index] + "\r\n");
}
}
public static void main(String[] args) {
MergeFile merge = new MergeFile();
if (null != args args.length 2) {// 输入参数合法,则使用,否则按照默认
merge.afile = args[0];
merge.bfile = args[1];
merge.mergefile = args[2];
} else {
System.out.println("Using the default file");
}
Scanner input = null;
int[] a = null;
int[] b = null;
int[] all = null;
try {
input = new Scanner(new File(merge.afile));
a = merge.readFile(input);
input = new Scanner(new File(merge.bfile));
b = merge.readFile(input);
all = merge.merge(a, b);
PrintStream print = new PrintStream(new File(merge.mergefile));
merge.writeFile(print, all);
} catch (Exception e) {
e.printStackTrace();
}
}
}
网页题目:java两个文件合并代码 java多个类合并为一个源文件
本文URL:http://lswzjz.com/article/hidise.html