導航:首頁 > 文件教程 > java合並文件夾

java合並文件夾

發布時間:2021-03-03 01:25:24

『壹』 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"});
}
}

『貳』 java 將不同文件下的相同多級目錄下文件內容,合並到一個新的文件夾中,建立對應多級目錄

用到的類可能有:File,BufferedReader
File:檢查文件或文件夾是否存在;創建文內件或文件夾;列出當容前目錄下的所有文件和文件夾;
BufferedReader:關鍵有一個readLine()方法,一次讀取一行。你的需求中說要刪除相同行的內容,要用到這個。
具體的用法你看下API。

『叄』 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中怎樣實現文件內容合並

FileWriter類的構造方法,就有一個參數是直接追加到文件末尾寫入的
------------------------
FileWriter
public FileWriter(File file,
boolean append)
throws IOException根據給定的 File 對象回構造答一個 FileWriter 對象。如果第二個參數為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處。

參數:
file - 要寫入數據的 File 對象
append - 如果為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處
拋出:
IOException - 如果該文件存在,但它是一個目錄,而不是一個常規文件;或者該文件不存在,但無法創建它;抑或因為其他某些原因而無法打開它
從以下版本開始:
1.4

『伍』 設計JAVA程序Combine.java, 將兩個文件合並成一個文件命名為CC.TXT

我這一個小程序
裡面有兩個函數
一個是讀文件的,一個是寫文件的!
你應該用得著!

/*******************************/

package rain.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 提供對文件的一些常用操作
* @author Rain
*
*/
public class FileOperate {
/**
* 將文件的內容讀取到一個String字元串中並返回
* @param Path String文件路徑
* @return 文件的內容
*/
public static String getContent(String path)
{
java.io.FileReader reader = null;
BufferedReader buf= null;
StringBuffer content = new StringBuffer();;
try
{
reader = new java.io.FileReader(path);
buf = new BufferedReader(reader);//建立BufferedReader對象,並實例化為br
String line=buf.readLine();//從文件讀取一行字元串
while(line != null)
{
content.append(line+"\n");
line=buf.readLine();//從文件中繼續讀取一行數據
}
}
catch(Exception ex)
{
return "";
}
finally
{
try{buf.close();}catch(Exception ex2){}
try{reader.close();}catch(Exception ex2){}
}
return content.toString();
}
/**
* 向文件裡面寫入內容,
* @param file File文件
* @param content String要寫入的內容
* @param append boolean是否為追加模式寫入 true為追加模式 false為覆蓋
* @throws FileNotFoundException 文件未找到,拋出異常,通常是其目錄路徑不存在
*/
public void writeFile(File file,String content,boolean append) throws FileNotFoundException{
FileOutputStream fos=null;
PrintWriter pw = null;
try {
fos = new FileOutputStream(file,append);
pw = new PrintWriter(fos);
pw.print(content);
pw.flush();
} finally{
pw.close();
try {if(fos!=null)fos.close();} catch (IOException e) {}
}
}
}

『陸』 如何使用Java合並多個文件

使用java編程語言,對文件進行操作,合並多個文件,代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

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();

『柒』 java中如何將兩個文件合並到另一個文件

java可以使用FileChannel快速高效地將多個文件合並到一起,以下是詳細代碼:

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=newFileInputStream(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){}
}
}
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/in_1.txt","D:/in_2.txt","D:/in_3.txt"});
}
}

『捌』 (高分)怎樣講手機的幾個java文件合並成一個java文件

件分割合並工具的Java源碼2007/02/21 05:38 A.M.public class FileCut
/*cl_tl*/
{
private Shell shell;
private Display display;
private Text txtSourceFile;
private Text txtNewFilePath;
private Text txtFileSize;
private Button btnChooseFile;
private Button btnChoosePath;
private Button btnCut;
private Button btnUnionFiles;
private Button btnUp;
private Button btnDown;
private Button btnClear;
private Button btnClearAll;
private Button btnUnion;
private Table tblFileList;

private File sourceFile = null; /*源文件*/
private File[] newFile = null; /*分割後產生的文件*/
private int fileCount = 0; /*分割成的文件個數*/
private int fileSize = 0; /*分割的文件塊大小*/
private String strSourceFile = null; /*源文件路徑及名稱*/
private String strNewFilePath = null; /*分割文件存放路徑*/

public static void main(String[] args)
{
Display display=new Display();
FileCut Item=new FileCut();
Item.createShell();

while( !Item.shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

/*fn_hd
*rem:創建窗體
*aut:
*log:2005-11-24
*/
private void createShell()
/*fn_tl*/
{
shell = new Shell(display, SWT.MIN);
shell.setBounds(300,250,500,330);
shell.setText("文件分割合並");
GridLayout shellLayout = new GridLayout();
shellLayout.numColumns = 3;
shell.setLayout(shellLayout);
createWidgets();
shell.open();
}

/**fn_hd
*rem:在窗體內添加控制項
*per:
*aut:
*log:2005-11-24
*/
private void createWidgets()
/*fn_tl*/
{
final Label lblNull0 = new Label(shell,SWT.None);
GridData gd0 = new GridData();
gd0.horizontalSpan = 3;
lblNull0.setLayoutData(gd0);

final Label lblSourceFile = new Label(shell, SWT.None);
lblSourceFile.setText("源 文 件");

GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
txtSourceFile = new Text(shell, SWT.BORDER);
txtSourceFile.setLayoutData(gd2);

btnChooseFile = new Button(shell, SWT.PUSH);
btnChooseFile.setText("..");

final Label lblPath = new Label(shell, SWT.None);
lblPath.setText("存放路徑");

txtNewFilePath = new Text(shell, SWT.BORDER);
GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
txtNewFilePath.setLayoutData(gd3);

btnChoosePath = new Button(shell, SWT.PUSH);
btnChoosePath.setText("..");

final Label lblSize = new Label(shell, SWT.None);
lblSize.setText("分塊大小(KB)");

txtFileSize = new Text(shell,SWT.BORDER);
GridData gd7 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
txtFileSize.setLayoutData(gd7);

btnCut = new Button(shell, SWT.PUSH);
GridData gd4 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
btnCut.setLayoutData(gd4);
btnCut.setText("開始分割");

final Label lbl1 = new Label(shell, SWT.None);
GridData gd8 = new GridData();
gd8.horizontalSpan = 3;
lbl1.setLayoutData(gd8);
lbl1.setText("待合並的文件列表");

tblFileList = new Table(shell, SWT.BORDER);
GridData gd1 = new GridData(GridData.FILL_BOTH);
gd1.horizontalSpan = 2;
tblFileList.setLayoutData(gd1);

Composite com = new Composite(shell, SWT.None);
GridLayout comLayout = new GridLayout();
com.setLayout(comLayout);

final Label lblNote = new Label(shell, SWT.None);
GridData data = new GridData();
data.horizontalSpan=3;
lblNote.setLayoutData(data);
lblNote.setText("提示:注意合並文件的順序");

btnUnionFiles = new Button(com, SWT.PUSH);
btnUnionFiles.setText("選擇文件");

btnUp = new Button(com, SWT.PUSH);
btnUp.setText(" 上移 ");
btnUp.setEnabled(false);

btnDown = new Button(com, SWT.PUSH);
btnDown.setText(" 下移 ");
btnDown.setEnabled(false);

btnClear = new Button(com, SWT.PUSH);
btnClear.setText(" 刪除 ");
btnClear.setEnabled(false);

btnClearAll = new Button(com, SWT.PUSH);
btnClearAll.setText("清空列表");

btnUnion = new Button(com, SWT.PUSH);
btnUnion.setText("開始合並");

btnCut.addSelectionListener(new SelectionAdapter()
//添加"開始分割"監視器
{
public void widgetSelected(SelectionEvent event)
{
cutButtonEvent();
}
});

btnChooseFile.addSelectionListener(new SelectionAdapter()
//添加選擇(源文件)監視器
{
public void widgetSelected(SelectionEvent event)
{
FileDialog fdOpen = new FileDialog(shell,SWT.OPEN);
String strFileName = fdOpen.open();
if (strFileName != null)
{
txtSourceFile.setText(strFileName);
txtNewFilePath.setText(fdOpen.getFilterPath());
txtFileSize.setFocus();
}
}
});

btnChoosePath.addSelectionListener(new SelectionAdapter()
//添加選擇(分割文件存放路徑)監視器
{
public void widgetSelected(SelectionEvent event)
{
DirectoryDialog dirDia = new DirectoryDialog(shell);
String strFileDir = dirDia.open();
if (strFileDir != null)
{
txtNewFilePath.setText(strFileDir);
txtFileSize.setFocus();
}
}
});

btnUp.addSelectionListener(new SelectionAdapter()
//添加"上移"監視器
{
public void widgetSelected(SelectionEvent event)
{
//int[] itemIndices = tblFileList.getSelectionIndices();
int itemIndex = tblFileList.getSelectionIndex();
if (itemIndex == 0)
{
tblFileList.setFocus();
return;
}
//交換列表中兩行的內容
String strTemp = tblFileList.getItem(itemIndex).getText();
tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
itemIndex - 1).getText());
tblFileList.getItem(itemIndex - 1).setText(strTemp);
//設置焦點
tblFileList.setSelection(itemIndex - 1);
tblFileList.setFocus();
}
});

btnDown.addSelectionListener(new SelectionAdapter()
//添加"下移"監視器
{
public void widgetSelected(SelectionEvent event)
{
//int[] itemIndices = tblFileList.getSelectionIndices();
int itemIndex = tblFileList.getSelectionIndex();
if (itemIndex == tblFileList.getItemCount() - 1)
{
tblFileList.setFocus();
return;
}
//交換列表中兩行的內容
String strTemp = tblFileList.getItem(itemIndex).getText();
tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
itemIndex + 1).getText());
tblFileList.getItem(itemIndex + 1).setText(strTemp);
//設置焦點
tblFileList.setSelection(itemIndex + 1);
tblFileList.setFocus();
}
});

btnClear.addSelectionListener(new SelectionAdapter()
//添加"刪除"監視器
{
public void widgetSelected(SelectionEvent event)
{
int itemIndex = tblFileList.getSelectionIndex();
tblFileList.remove(itemIndex);
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
}
});

btnClearAll.addSelectionListener(new SelectionAdapter()
//添加"清空列表"監視器
{
public void widgetSelected(SelectionEvent event)
{
tblFileList.removeAll();
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
}
});

txtFileSize.addSelectionListener(new SelectionAdapter()
//添加"分塊大小"文本框中輸入回車監視器
{
public void widgetDefaultSelected(SelectionEvent event)
{
cutButtonEvent();
}
});

btnUnionFiles.addSelectionListener(new SelectionAdapter()
//添加"選擇文件"監視器
{
public void widgetSelected(SelectionEvent event)
{
FileDialog fd = new FileDialog(shell, SWT.MULTI);
fd.setFilterExtensions(new String[]{"*.dat", "*.*"});
if (fd.open() != null)
{
String[] strFiles = fd.getFileNames();
String strFilePath = fd.getFilterPath();
//strUnionFilePath = new String[strFiles.length];
for(int i = 0; i < strFiles.length; i++)
{
//strUnionFilePath[i] = fd.getFilterPath();
TableItem item = new TableItem(tblFileList, SWT.None);
item.setText(strFilePath + "\\" + strFiles[i]);
}
}
}
});

btnUnion.addSelectionListener(new SelectionAdapter()
//添加"開始合並"監視器
{
public void widgetSelected(SelectionEvent event)
{
if (tblFileList.getItemCount() == 0)
{
return;
}
switch (unionFiles())
{
case 1:
showMessage("成功", "合並完成!",
SWT.OK | SWT.ICON_INFORMATION);
tblFileList.removeAll();
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
break;
case -1:
showMessage("錯誤", "文件不存在!",
SWT.OK | SWT.ICON_ERROR);
break;
case -2:
break;
default:
showMessage("錯誤", "有錯誤發生,文件合並失敗!",
SWT.OK | SWT.ICON_ERROR);
}
}
});

tblFileList.addSelectionListener(new SelectionAdapter()
//添加選中列表中元素監視器
{
public void widgetSelected(SelectionEvent event)
{
btnUp.setEnabled(true);
btnDown.setEnabled(true);
btnClear.setEnabled(true);
}
});
}

/**fn_hd
*rem:顯示消息框
*log:2005-11-24
*/
private int showMessage(String strText, String strMessage, int i)
{/*fn_tl*/
MessageBox msgBox = new MessageBox(shell,i);
msgBox.setText(strText);
msgBox.setMessage(strMessage);
return msgBox.open();
}

/**fn_hd
*rem:點擊"分割"按鈕觸發的事件響應
*log:2005-11-24
*/
private void cutButtonEvent()
/*fn_tl*/
{
strSourceFile = txtSourceFile.getText().trim();
strNewFilePath = txtNewFilePath.getText().trim();

if (strSourceFile.equals("") || strNewFilePath.equals(""))
{
showMessage("提示", "請輸入源文件和 \n\n分割文件的路徑! ",
SWT.OK | SWT.ICON_INFORMATION);
return;
}
try
{
fileSize = Integer.parseInt(txtFileSize.getText());
fileSize *= 1024;
if (fileSize <= 0)
{
showMessage("錯誤", "分塊大小為正整數! ",
SWT.OK | SWT.ICON_ERROR);
return;
}
}
catch(Exception e)
{
showMessage("錯誤", "請在分塊大小框填入數字! ",
SWT.OK | SWT.ICON_ERROR);
return;
}
switch (cutFile())
{
case 1:
showMessage("提示", "分割完成! ", SWT.OK |
SWT.ICON_INFORMATION);
txtSourceFile.setText("");
txtNewFilePath.setText("");
txtFileSize.setText("");
break;
case -1:
showMessage("錯誤", "源文件不存在或存放路徑不存在!",
SWT.OK | SWT.ICON_ERROR);
break;
default:
showMessage("未知錯誤", "文件分割失敗! ",
SWT.OK | SWT.ICON_ERROR);
}
}

/*fn_hd
*rem:文件分割實現
*per:成功返回1,文件未找到返回-1,其他情況返回0
*exp:IOException
*aut:
*log:2005-11-22,創建
*log:2005-11-24,修改
*/
private int cutFile()
/*fn_tl*/
{
sourceFile = new File(strSourceFile);
fileCount = (int) (sourceFile.length() / fileSize);
if (sourceFile.length() % fileSize != 0)
{
fileCount++;
}
newFile = new File[fileCount];

try
{
int count = 0;
int i = 0;
byte[] bueff = new byte[fileSize];
FileOutputStream out = null;
FileInputStream in = new FileInputStream(sourceFile);
for (i = 0; i < newFile.length; i++)
{
newFile[i] = new File(strNewFilePath,
i + sourceFile.getName() + ".dat");
}
i = 0;
while ((count = in.read(bueff,0,fileSize)) != -1)
{
out = new FileOutputStream(newFile[i]);
out.write(bueff,0,count);
out.close();
i++;
}
in.close();
return 1;
}
catch(FileNotFoundException e)
{
System.out.println(e);
return -1;
}
catch(IOException e)
{
System.out.println(e);
return 0;
}
}

/*fn_hd
*rem:文件合並的實現
*per:成功返回1,文件未找到返回-1,取消操作返回-2,其他情況返回0;
*aut:
*exp:FileNotFoundException,IOException
*log:2005-11-28,創建
*/
private int unionFiles()
/*fn_tl*/
{
String[] strFiles = new String[tblFileList.getItemCount()];
File[] unionFiles = new File[strFiles.length];
FileDialog fdSave = new FileDialog(shell, SWT.SAVE);
String s = fdSave.open();
if (s == null)
{
return -2;
}
File outFile = new File(fdSave.getFilterPath(),
fdSave.getFileName());
if (outFile.exists())
{
int msg = showMessage("提示", "該文件以存在,是否替換?",
SWT.YES | SWT.NO | SWT.ICON_QUESTION);
if (msg == SWT.NO)
{
return -2;
}
}
for(int i = 0; i < strFiles.length; i++)
{
strFiles[i] = tblFileList.getItem(i).getText();
}
try
{
FileInputStream in = null;
FileOutputStream out = new FileOutputStream(outFile);
byte[] buff = new byte[1024];
int count;
for(int i = 0; i < strFiles.length; i++)
{
in = new FileInputStream(strFiles[i]);
while((count = in.read(buff,0,1024)) != -1)
{
out.write(buff,0,count);
}
in.close();
}
out.close();
return 1;
}
catch(FileNotFoundException e)
{
System.out.println(e);
return -1;
}
catch(IOException e)
{
System.out.println(e);
return 0;
}
}

『玖』 用Java:將兩個文件中的內容合並到一個文件夾中,要求用字元流

這個程序 我用C++寫過。java的原理也是一樣的。你先用程序打開一個文件,然後順版序把兩個文件的內容寫入其權中就可以了。
你要C++的代碼的話,我可以給你。java很久沒有弄了,也不太想專門去幫你寫一個。我覺得要學程序就應該自己寫,不會的地方可以問別人思想,但不能讓別人幫你寫完,除非你只想要那個程序。如果這樣的話,用什麼編寫的是無所胃的。

『拾』 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");
}

閱讀全文

與java合並文件夾相關的資料

熱點內容
win10plsql 瀏覽:819
香港電影開頭一個女的在床上自慰 瀏覽:512
win10cdromsys下載 瀏覽:30
桌面程序hibernate 瀏覽:14
如何建蔬菜網站 瀏覽:579
android網路通信聊天 瀏覽:1
電影頭上裹著布還有紐扣 瀏覽:246
iphone6nfc充電 瀏覽:422
鐵銹戰爭的文件夾是哪個 瀏覽:184
大數據業務描述 瀏覽:162
古惑仔粵語版歌詞 瀏覽:897
韓國劇情片網站 瀏覽:759
自學滅火器編程該如何入手 瀏覽:817
網站ip地址怎麼防禦 瀏覽:572
大數據自動化部署 瀏覽:368
自動編程軟體有哪些有什麼特色 瀏覽:140
韓國污片網站 瀏覽:758
主角要收集各種女子 瀏覽:463
《哈佛女孩》電影 瀏覽:422

友情鏈接