java合并文件夹
A. 写一个java程序用于合并文件
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Read {
String file1Path = "D:/First.txt";
String file2Path = "D:/Last.txt";
String resultPath = "D:/Result.txt";
public static void main(String[] args) throws IOException{;
Read r = new Read();
r.readFile();
}
public void readFile() throws IOException{
BufferedReader in1 = new BufferedReader(new FileReader(file1Path));
BufferedReader in2 = new BufferedReader(new FileReader(file2Path));
String line1 = null, line2 = null;
String s1 = "", s2 = "", s3 = "";
int length;
char a,b;
FileWriter fw = null;
fw = new FileWriter(resultPath,false);
boolean flag = true;
while(flag){
line1 = in1.readLine();
line2 = in2.readLine();
//宏信悉当First.txt和Last.txt行数不同时坦迟
if(line1 == null && line2 != null){
s2 += line2;
flag = false;
continue;
}
if(line2 == null && line1 != null){
s3 += line1;
flag = false;
continue;
}
if(line1 == null && line2 == null){
flag = false;
continue;
}
length = (line1.length() < line2.length())?line1.length():line2.length();
for(int i = 0; i < length; i ++){
a = line1.charAt(i);
b = line2.charAt(i);
if(a == b){
s1 += a;
}else if(a != b){
s2 += a;
s3 += b;
}
}
//当蔽乎一行字符串长度不同时
if(line1.length() > line2.length()){
s2 += line1.substring(line2.length(), line1.length());
}else if(line1.length() < line2.length()){
s3 += line2.substring(line1.length(), line2.length());
}
}
fw.write(s1 + "\r\n来自文件一:" + s2 + "\r\n来自文件二:" + s3);
fw.close();
}
}
B. 如何快速的合并pdf文件 java
将多个PDF文件合并到一起,首先需要有相关软件的帮助,那就是迅捷pdf合并软件。用户电脑上下载安装迅捷PDF合并软件,运行迅捷PDF软件,选择将文件合并为PDF,然后就是添加文件到软件中,支持多个PDF文件的批量转换,点击“添加文件”或者将文件直接拖拽到软件中,添加完成后点击右下角的“合并软件”即可。用户还可以选择合并后的文件的保存路径。只需稍等片刻余老指,迅捷PDF合并软件就可以完成多个文件的合并,合并完成后会自动保存到你所自定义的文件夹中。
方法二:在PDF文件里:文档--插入页面,进入要选择插入的文件,选定后,选择拟插入文件的位置,最后确定即可,非常方便。
方法二:使用pdfFactory软件,到网上竖配下载后,安装到电脑上,这时在“打印机和传真”里出现一个pdfFactory Pro的打印机。合并方法:1、首先将pdfFactory Pro的打印机设为默认打印机;2、选中需含衫要合并的几个文件,点右键打印,出现打印任务;3、在任务里调整合并文件的顺序,最后保存PDF文件,合并文件结束。
C. JAVA合并两个文件并去重
先做一个buffer:
StringBuilder sb_a = new StringBuilder(); // for a
StringBuilder sb_b = new StringBuilder(); // for b
然后读文件:
Path aFile = Paths.get("a.txt");
try (BufferedReader reader = Files.newBufferedReader( aFile, Charset.defaultCharset())) {
String lineFromFile = "";
while((lineFromFile = reader.readLine()) != null){
sb_a.append(lineFromFile);
}
} catch(IOException exception) {
System.out.println("Error while reading file");
}
b.txt也是同一个方法
跟着以你自定义的方态正法去比较两个怎样合拼,暂存裤嫌为String merged = ...;
最胡闭手后写回文件:
Path abFile = Paths.get("ab.txt");
try (BufferedWriter writer = Files.newBufferedWriter( abFile, Charset.defaultCharset())) {
writer.append(merged);
writer.newLine(); // optional
writer.flush();
} catch(IOException exception) {
System.out.println("Error writing to file");
}
D. java合并两个txt文件并生成新txt
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.PrintStream;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.LinkedList;
importjava.util.Scanner;
publicclassTest
{
publicstaticfinalStringLINE=System.getProperty("line.separator");
publicstaticint[]readfile(Scannerinput)
{
int[]a=newint[0];
while(input.hasNextLine())
{
Stringline=input.nextLine().trim();
int[]dest=newint[a.length+1];
System.array(a,0,dest,0,a.length);
dest[dest.length-1]=Integer.parseInt(line);
a=dest;
}
input.close();
returna;
}
publicstaticvoidwritefile(PrintStreamoutput,int[]a)
{
output.append(Arrays.toString(a).replaceAll("[\[\]\s]","").replaceAll(",",LINE));
output.flush();
output.close();
}
publicstaticint[]merge(int[]a,int[]b)
{
int[]dest=newint[a.length+b.length];
System.array(a,0,dest,0,a.length);
System.array(b,0,dest,a.length,b.length);
returndest;
}
}
classTest1
{
publicstaticvoidmain(String[]args)
{
if(args.length!=3)
{
System.err.println("输入的参数个数不是3个");
return;
}
try
{
Scannerinput=newScanner(newFile(args[0]));
int[]a=Test.readfile(input);
input=newScanner(newFile(args[1]));
int[]b=Test.readfile(input);
int[]dest=Test.merge(a,b);
try
{
PrintStreamoutput=newPrintStream(args[2]);
Test.writefile(output,dest);
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}
classTest2
{
publicstaticvoidmain(String[]args)
{
if(args.length!=1)
{
System.err.println("输入的参数个数不是1个");
return;
}
try
{
Scannerinput=newScanner(newFile(args[0]));
int[]dest=Test.readfile(input);
Arrays.sort(dest);
PrintStreamoutput=newPrintStream(args[0]);
Test.writefile(output,dest);
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}
<Test3>
{
inti;
publicTest3(inti)
{
this.i=i;
}
@Override
publicintcompareTo(Test3o)
{
if(o.i>i)
{
return-1;
}
elseif(o.i<i)
{
return1;
}
else
{
return0;
}
}
@Override
publicStringtoString()
{
returnString.format("%s",i);
}
publicstaticvoidmain(String[]args)
{
if(args.length!=3)
{
System.err.println("输入的参数个数不是3个");
return;
}
LinkedList<Test3>list=newLinkedList<Test3>();
try
{
Scannerinput1=newScanner(newFile(args[0]));
Scannerinput2=newScanner(newFile(args[1]));
while(true)
{
try
{
inta=Integer.parseInt(input1.nextLine().trim());
intb=Integer.parseInt(input2.nextLine().trim());
Test3ta=newTest3(a);
Test3tb=newTest3(b);
list.add(ta);
list.add(tb);
}
catch(Exceptione)
{
break;
}
}
input1.close();
input2.close();
Collections.sort(list);
PrintStreamoutput=newPrintStream(args[2]);
output.append(list.toString().replaceAll("[\[\]\s]","").replaceAll(",",Test.LINE));
output.flush();
output.close();
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}
E. java如何合并多个大的txt文件(每个文件50M)。nio处理文件如何提高速度
这种情况java.io, nio没有大区别
byte[]buf=newbyte[8*1024];
try(OutputStreamout=newnewFileOutputStream(outfile)){
for(Filef:txtFiles){
好拿try(FileInputStreamin=newFileInputStream(f)){
org.apache.commons.io.IOUtils.Large(in,out,buf);
核纳}
}
}
要是linux下,shell里直接执行cat *.txt >友氏搭out.txt就可以,不用写代码
F. java 将不同文件下的相同多级目录下文件内容,合并到一个新的文件夹中,建立对应多级目录
代码不写了,思路:
首先读取文件,依次读取所有目录,然后递归子目录,将所有目录下以a|b名称开头的文件内容分别按照行读取到a|b的ArrayList中,循环所有的ArrayList ,将每个内容都put到HashSet中去,这样,就得到你要的结果,最终碰丛输出到c目录下
当然,可以不读唯吵誉取到指段ArrayList中,直接put到HashSet,不过这样的话,就无法查找每个文件的原始内容。建议先分文件读取到多个ArrayList ,但不影响你的程序结果(可能会影响最终内容的顺序)
G. 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) {}
}
}
public static void main(String[] args) {
mergeFiles("D:/output.txt", new String[]{"D:/in_1.txt", "D:/in_2.txt", "D:/in_3.txt"});
}
}
H. 如何使用java合并多个文件
使用java编程语言,对文件进行操作,合并多个文件,代码如下:
importstaticjava.lang.System.out;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.Arrays;
publicclasstest{
publicstaticfinalintBUFSIZE=1024*8;
publicstaticvoidmergeFiles(StringoutFile,String[]files){
FileChanneloutChannel=null;
out.println("Merge"+Arrays.toString(files)+"into"+outFile);
try{
outChannel=newFileOutputStream(outFile).getChannel();
for(Stringf:files){
FileChannelfc=new尘李FileInputStream(f).getChannel();
芹碰ByteBufferbb=ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb)!=-1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!!");
嫌兄谈}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{if(outChannel!=null){outChannel.close();}}catch(IOExceptionignore){}
}
}
//下面代码是将D盘的1.txt2.txt3.txt文件合并成out.txt文件。
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"});
}
}
I. 用java io流把多个txt文件的内容合并到一个文件里
参考代悄春码如下:
public static void mergeFiles(String outFile, String[] files)
第一个参数是合并后生成文件的路早运枝径
第二个参数是你需要合并的文本文件列表
代码:
packageorg.lq.util;
importstaticjava.lang.System.out;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.CharBuffer;
importjava.nio.channels.FileChannel;
importjava.nio.charset.Charset;
importjava.nio.charset.CharsetDecoder;
importjava.nio.charset.CharsetEncoder;
importjava.util.Arrays;
publicclassMergeFile{
publicstaticfinalintBUFSIZE=1024*8;
publicstaticvoidmergeFiles(StringoutFile,String[]files){
FileChanneloutChannel=null;
out.println("Merge"+Arrays.toString(files)+"into"+outFile);
try{
outChannel=newFileOutputStream(outFile).getChannel();
for(Stringf:files){
Charsetcharset=Charset.forName("utf-8");
CharsetDecoderchdecoder=charset.newDecoder();
CharsetEncoderchencoder=charset.newEncoder();
FileChannelfc=newFileInputStream(f).getChannel();
ByteBufferbb=ByteBuffer.allocate(BUFSIZE);
CharBuffercharBuffer=chdecoder.decode(bb);
ByteBuffernbuBuffer=chencoder.encode(charBuffer);
while(fc.read(nbuBuffer)!=-1){
bb.flip();
nbuBuffer.flip();
outChannel.write(nbuBuffer);
bb.clear();
nbuBuffer.clear();
}
fc.close();
}
out.println("Merged!!");
}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{if(outChannel!=null){outChannel.close();}}catch(IOException陆敏ignore){}
}
}
}