基本信息
源码名称:java合并txt文件(swing项目)
源码大小:9.42KB
文件格式:.rar
开发语言:Java
更新时间:2019-07-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
通过输入文件路劲,将此路劲下文件合并
package com.myccb;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
class MyWindow {
private Frame f;
private TextField tf;
private JButton but;
private JButton hebing;
private TextArea ta;
private String newFileName="newFileName.txt";
private String newToFileName="newToFileName.txt";
MyWindow() {
init();
}
public void init() {
f = new Frame("合并文件窗口");//创建窗体对象
f.setBounds(300, 100, 600, 500);//设置窗体位置和大小
f.setLayout(new FlowLayout());//设置窗体布局为流式布局
tf = new TextField(60);//创建单行文本对象60长度大小字符
but = new JButton("转到此目录");//创建按钮对象
hebing=new JButton("合并文件");//创建按钮对象
ta = new TextArea(25, 70);//创建多行文本对象25行,70列
f.add(tf);//单行文本添加到窗体上
f.add(but);//按钮添加到窗体上
f.add(ta);//多行文本添加到窗体上
f.add(hebing);
myEvent();//加载事件处理
myEventHb();//加载事件处理
f.setVisible(true);//设置窗体可见
}
private void myEvent() {
//按钮事件监听器
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String dirPath=tf.getText();//获取单行文本内容保存到字符串dirPath中
File dir=new File(dirPath);//将字符串dirPath封装成文件
ta.setText(null);
//如果文件存在,而且是个目录执行下列操作
if(dir.exists() &&dir.isDirectory()){
ta.setText(null);//没打开一个目录前清空多行文本内容
String[] names=dir.list();//文件目录列表存放到字符数组中
StringBuffer bf=new StringBuffer();
for(String name :names){
bf.append(name "\r\n");//追加文本内容并换行
}
ta.setText(bf.toString());
}else if(!dir.exists()){
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不存在");//追加文本内容并换行
}else{
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不是文件夹");//追加文本内容并换行
}
}
});
//窗体关闭监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void myEventHb() {
//按钮事件监听器
hebing.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String dirPath=tf.getText();//获取单行文本内容保存到字符串dirPath中
if(!dirPath.endsWith("/")&&!dirPath.endsWith("\\")){
dirPath =File.separator;
}
File dir=new File(dirPath);//将字符串dirPath封装成文件
ta.setText(null);
//如果文件存在,而且是个目录执行下列操作
File file_new=new File(dirPath newFileName);
File file_to=new File(dirPath newToFileName);
if(file_new.exists()){
ta.setText("明细文件已经合成,请勿重复操作!");//追加文本内容并换行
return;
}
if(file_to.exists()){
ta.setText("汇总文件已经合成,请勿重复操作!");//追加文本内容并换行
return;
}
if(dir.exists() &&dir.isDirectory()){
ta.setText(null);//没打开一个目录前清空多行文本内容
String[] names=dir.list();//文件目录列表存放到字符数组中
for(String name :names){
if(name.endsWith("detail.txt")){
boolean flag=writeToFile(dirPath name,dirPath newFileName);
if(!flag){
ta.setText("合并文件出现异常!请删除新文件后重新操作!");//追加文本内容并换行
}
}
if(name.endsWith("total.txt")){
boolean flag=writeToFile(dirPath name,dirPath newToFileName);
if(!flag){
ta.setText("合并文件出现异常!请删除新文件后重新操作!");//追加文本内容并换行
}
}
}
ta.setText("成功合并文件!");//追加文本内容并换行
}else if(!dir.exists()){
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不存在,未合并文件,请指定正确的文件目录");//追加文本内容并换行
}else{
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不是文件夹,未合并文件,请指定正确的文件目录");//追加文本内容并换行
}
}
});
//窗体关闭监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* 从老文件里的类容复制到新文件下
* @param fromPath
* @param newPath
* @return
*/
public static boolean writeToFile(String fromPath,String newPath){
BufferedReader br=null;
OutputStreamWriter outputStreamWriter=null;
FileOutputStream fileOutputStream=null;
try{
StringBuffer bf=new StringBuffer();
File fromFile =new File(fromPath);
File newFile =new File(newPath);
if(!fromFile.exists()){
return false;
}else{
if(!newFile.exists()){
newFile.createNewFile();
}
br=new BufferedReader(new InputStreamReader(new FileInputStream(fromFile),"UTF-8"));
String data=null;
while((data=br.readLine())!=null){
bf.append(data);
bf.append("\n");
}
fileOutputStream=new FileOutputStream(newFile,true);
outputStreamWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");//
outputStreamWriter.write(bf.toString());
outputStreamWriter.flush();
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStreamWriter!=null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] args){
new MyWindow();
}
}
通过输入文件路劲,将此路劲下文件合并
package com.myccb;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
class MyWindow {
private Frame f;
private TextField tf;
private JButton but;
private JButton hebing;
private TextArea ta;
private String newFileName="newFileName.txt";
private String newToFileName="newToFileName.txt";
MyWindow() {
init();
}
public void init() {
f = new Frame("合并文件窗口");//创建窗体对象
f.setBounds(300, 100, 600, 500);//设置窗体位置和大小
f.setLayout(new FlowLayout());//设置窗体布局为流式布局
tf = new TextField(60);//创建单行文本对象60长度大小字符
but = new JButton("转到此目录");//创建按钮对象
hebing=new JButton("合并文件");//创建按钮对象
ta = new TextArea(25, 70);//创建多行文本对象25行,70列
f.add(tf);//单行文本添加到窗体上
f.add(but);//按钮添加到窗体上
f.add(ta);//多行文本添加到窗体上
f.add(hebing);
myEvent();//加载事件处理
myEventHb();//加载事件处理
f.setVisible(true);//设置窗体可见
}
private void myEvent() {
//按钮事件监听器
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String dirPath=tf.getText();//获取单行文本内容保存到字符串dirPath中
File dir=new File(dirPath);//将字符串dirPath封装成文件
ta.setText(null);
//如果文件存在,而且是个目录执行下列操作
if(dir.exists() &&dir.isDirectory()){
ta.setText(null);//没打开一个目录前清空多行文本内容
String[] names=dir.list();//文件目录列表存放到字符数组中
StringBuffer bf=new StringBuffer();
for(String name :names){
bf.append(name "\r\n");//追加文本内容并换行
}
ta.setText(bf.toString());
}else if(!dir.exists()){
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不存在");//追加文本内容并换行
}else{
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不是文件夹");//追加文本内容并换行
}
}
});
//窗体关闭监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void myEventHb() {
//按钮事件监听器
hebing.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String dirPath=tf.getText();//获取单行文本内容保存到字符串dirPath中
if(!dirPath.endsWith("/")&&!dirPath.endsWith("\\")){
dirPath =File.separator;
}
File dir=new File(dirPath);//将字符串dirPath封装成文件
ta.setText(null);
//如果文件存在,而且是个目录执行下列操作
File file_new=new File(dirPath newFileName);
File file_to=new File(dirPath newToFileName);
if(file_new.exists()){
ta.setText("明细文件已经合成,请勿重复操作!");//追加文本内容并换行
return;
}
if(file_to.exists()){
ta.setText("汇总文件已经合成,请勿重复操作!");//追加文本内容并换行
return;
}
if(dir.exists() &&dir.isDirectory()){
ta.setText(null);//没打开一个目录前清空多行文本内容
String[] names=dir.list();//文件目录列表存放到字符数组中
for(String name :names){
if(name.endsWith("detail.txt")){
boolean flag=writeToFile(dirPath name,dirPath newFileName);
if(!flag){
ta.setText("合并文件出现异常!请删除新文件后重新操作!");//追加文本内容并换行
}
}
if(name.endsWith("total.txt")){
boolean flag=writeToFile(dirPath name,dirPath newToFileName);
if(!flag){
ta.setText("合并文件出现异常!请删除新文件后重新操作!");//追加文本内容并换行
}
}
}
ta.setText("成功合并文件!");//追加文本内容并换行
}else if(!dir.exists()){
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不存在,未合并文件,请指定正确的文件目录");//追加文本内容并换行
}else{
ta.setText(null);//没打开一个目录前清空多行文本内容
ta.setText("你输入的目录不是文件夹,未合并文件,请指定正确的文件目录");//追加文本内容并换行
}
}
});
//窗体关闭监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* 从老文件里的类容复制到新文件下
* @param fromPath
* @param newPath
* @return
*/
public static boolean writeToFile(String fromPath,String newPath){
BufferedReader br=null;
OutputStreamWriter outputStreamWriter=null;
FileOutputStream fileOutputStream=null;
try{
StringBuffer bf=new StringBuffer();
File fromFile =new File(fromPath);
File newFile =new File(newPath);
if(!fromFile.exists()){
return false;
}else{
if(!newFile.exists()){
newFile.createNewFile();
}
br=new BufferedReader(new InputStreamReader(new FileInputStream(fromFile),"UTF-8"));
String data=null;
while((data=br.readLine())!=null){
bf.append(data);
bf.append("\n");
}
fileOutputStream=new FileOutputStream(newFile,true);
outputStreamWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");//
outputStreamWriter.write(bf.toString());
outputStreamWriter.flush();
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStreamWriter!=null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] args){
new MyWindow();
}
}