package com.komlin.component.task;
import com.komlin.component.consts.FilePathConsts;
import org.ibex.nestedvm.util.Seekable;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AppendFile {
public static void readTxtFile(){
File files = new File("F://test2");
if (files == null) {
return;
}
File[] tempList = files.listFiles();
if (tempList == null) {
return;
}
//System.out.println("该目录下对象个数:" + tempList.length);
List<FileTemp0> fileTempList = new ArrayList<FileTemp0>();
for (int i = 0;i < tempList.length; i++) {
FileTemp0 fileTemp0 = new FileTemp0();
fileTemp0.setFile(tempList[i]);
String str = tempList[i].getName();
str = str.substring(5, str.indexOf("."));
fileTemp0.setIndex(Integer.valueOf(str));
fileTempList.add(fileTemp0);
}
//排序 上传时间早的先处理
Collections.sort(fileTempList,new Comparator<FileTemp0>(){
@Override
public int compare(FileTemp0 o1, FileTemp0 o2) {
return o1.getIndex() < o2.getIndex() ? 1 : 0;
}
});
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < fileTempList.size(); i++) {
try {
String encoding="GBK";
File file=fileTempList.get(i).getFile();
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
stringBuffer.append(lineTxt + "\r\n");
//System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
method3("F://index.txt", stringBuffer.toString());
}
public static void method1(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用FileWriter
*
* @param fileName
* @param content
*/
public static void method2(String fileName, String content) {
FileWriter writer = null;
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(fileName, true);
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用RandomAccessFile
*
* @param fileName 文件名
* @param content 追加的内容
*/
public static void method3(String fileName, String content) {
RandomAccessFile randomFile = null;
try {
// 打开一个随机访问文件流,按读写方式
randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.write(content.getBytes("GBK"));
} catch (IOException e) {
e.printStackTrace();
} finally{
if(randomFile != null){
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
/* try{
File file = new File("d://text.txt");
if(file.createNewFile()){
System.out.println("Create file successed");
}
method1("d://text.txt", "123");
method2("d://text.txt", "123");
method3("d://text.txt", "123");
}catch(Exception e){
System.out.println(e);
}*/
readTxtFile();
//method3("F://index.txt", "大的");
}
}
class FileTemp0{
File file;
int index = 0;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
chm转txt软件
http://download.csdn.net/detail/u011078071/5586305
给蛋姐姐写的txt拼接
最新推荐文章于 2019-03-06 17:56:24 发布