导航:首页 > 文件教程 > 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合并文件夹相关的资料

热点内容
win8和linux双系统安装 浏览:328
苹果5按屏幕有紫色 浏览:272
qq已失效的文件怎么找回 浏览:63
步步高s7系统升级 浏览:179
win10双启动菜单 浏览:749
广州塔如何编程 浏览:817
如何提取指定数据到另外一列 浏览:934
macbook如何用自带软件编程 浏览:467
燕秀工具箱安装教程 浏览:995
进军大数据 浏览:480
单片机视频教程网盘 浏览:722
83描述文件还原 浏览:357
FindMyFriends安卓 浏览:899
2010word删除页眉横线 浏览:208
小程序名称问题 浏览:821
win10网吧专版 浏览:453
数据线哪个颜色的是txrx 浏览:664
微信打飞机有数据库吗 浏览:162
是什么编程世界 浏览:564
四川大数据成果 浏览:937

友情链接