當前位置:首頁 » 文件管理 » java合並文件夾

java合並文件夾

發布時間: 2023-04-09 17:46:51

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){}
}
}
}
熱點內容
安卓手機怎麼找微信隱藏對話 發布:2025-05-14 23:07:47 瀏覽:336
怎麼查看泰拉伺服器ip 發布:2025-05-14 23:03:29 瀏覽:72
c語言學生成績查詢系統 發布:2025-05-14 22:58:30 瀏覽:4
怎麼進別人的伺服器 發布:2025-05-14 22:45:55 瀏覽:772
用編程寫音樂 發布:2025-05-14 22:45:08 瀏覽:782
如何識別電腦的網路配置 發布:2025-05-14 22:38:46 瀏覽:847
pipforpython3 發布:2025-05-14 22:38:34 瀏覽:350
如何把迷你世界的伺服器搞崩 發布:2025-05-14 22:37:15 瀏覽:94
如何讓安卓卡死機 發布:2025-05-14 22:36:27 瀏覽:634
wemall微商城源碼 發布:2025-05-14 22:15:20 瀏覽:804