运行效果:
代码:
package com.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
public class CreateAndReadTxt {
// 文件名称
public static String fileName = ".txt";
public static String newFileName = ".txt";
// 文件路径
public final static String URL = System.getProperty("user.dir");
// CreateAndReadTxt.class.getResource("/").getPath();
// 创建TXT文件
public static void createTxtFile(String fName, String fileContent) {
// 创建文件
fileName = fName + fileName;
File file = new File(fileName);
// 可以更改
file.setWritable(true);
// 判断当前路径下是否存在同名文件
boolean isExist = file.exists();
if (isExist) {
// 文件存在,删除
file.delete();
}
// 写入文件
try {
// 文件写入对象
FileOutputStream fos = new FileOutputStream(file);
// 输入流写入----默认字符为GBK
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 写入
osw.write(fileContent);
// 写入完毕后关闭
osw.close();
System.out.println("成功创建文件:\t"+fileName);
} catch (IOException e) {
System.out.println("写入文件失败:\t" + e.getMessage());
}
}
// 阅读文件
public static void readFile(String fileName) {
System.out.println("开始读取文件:\t" + fileName);
// 产生文件对象
File file = new File(fileName);
//
try {
// 字符读取
FileReader fr = new FileReader(file);
// 缓冲处理
BufferedReader br = new BufferedReader(fr);
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
// 关闭
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
} catch (IOException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
}
}
// 文件复制
public static void copyFile(String fromFileName,String toFileName){
//读取文件
File file = new File(fromFileName);
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
// 定义接收变量
Vector vec = new Vector();
String s = "";
while(null!=(s=br.readLine())){
vec.add(Double.parseDouble(s));
}
br.close();
fr.close();
// 保存到数组并进行排序
Double dou[] = new Double[vec.size()];
vec.toArray(dou);
Arrays.sort(dou);
System.out.println("========复制文件=========");
// 写入新文件
newFileName = "副本"+newFileName;
File newFile = new File(toFileName);
FileOutputStream fos = new FileOutputStream(newFile, true);
OutputStreamWriter osm = new OutputStreamWriter(fos);
for(Double d:dou){
osm.write(d.doubleValue()+"\n");
}
osm.close();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
} catch (IOException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
}
}
public static void main(String[] args) {
/**
* 构造数据
*/
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String s = "";
while(!("end".equals(s = scan.next()))){// 当输入end时,结束
sb.append(s);
sb.append("\n");
}
scan.close();
/**
* 使用数据
*/
CreateAndReadTxt.createTxtFile("creat", sb.toString());
CreateAndReadTxt.readFile(fileName);
System.out.println(fileName);
CreateAndReadTxt.copyFile(fileName, newFileName);
CreateAndReadTxt.readFile(newFileName);
}
}