package 实验;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class 文件读取 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:/file/123.txt");
byte[] context = new byte[fis.available()];
fis.read(context);
String s = new String(context);
System.out.println(s);
}
}
package 实验;
import java.io.*;
import java.util.Scanner;
public class 文件写入 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入文本内容:");
String context = sc.next();
FileOutputStream fos = new FileOutputStream("D:/file/2.txt",true);
fos.write(context.getBytes());
fos.flush();
fos.close();
}
}
package 实验;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class 文件复制 {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("D:/file/2.txt");
byte[] context = new byte[fis.available()];
fis.read(context);
FileOutputStream fos =new FileOutputStream("D:/file/key/2.txt");
fos.write(context);
fos.flush();
fos.close();
}
}
package 实验;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class 日志生成代码 {
public static void main(String[] args) throws Exception{
try {
FileInputStream fis = new FileInputStream("D:/file/3.txt");
}catch (IOException e){
FileOutputStream fos = new FileOutputStream("D:/file/log/log.txt",true);
String date = new SimpleDateFormat("yyy-hh-dd HH:mm:ss").format(new Date());
String log = date + " "+e.getMessage()+"\r\n";
fos.write(log.getBytes());
fos.flush();
fos.close();
}
}
}