package com.aldd1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class TestTxt {
public static void main(String[] args) throws IOException {
File file=new File("D:\\java\\java高级homework3\\src\\com\\aldd1test.txt");
Scanner input=new Scanner(System.in);
FileInputStream fint=null;
FileOutputStream fout=null;
try {
if(file.exists()) {
System.out.println("doc目录下已有test.txt");
System.out.println("请输入添加内容:");
String content=input.next();
fout=new FileOutputStream(file, true);
byte[] bt=content.getBytes();
fout.write(bt);
System.out.println("添加成功!");
}
else {
fout=new FileOutputStream("D:\\java\\java高级homework3\\src\\com\\aldd1test.txt");
System.out.println("doc已经创建test.txt");
byte[] ch= {'I','a','m','i','r','o','n','m','a','n'};
fout.write(ch, 0, 10);
}
fint=new FileInputStream(file);
byte[] bt=new byte[fint.available()];
fint.read(bt);
String str=new String(bt);
System.out.println(str);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
input.close();
try {
if (null != fout) {
fout.close();
}
if (null != fint) {
fint.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史