原创,用eclipse编写代码,有疑问可以提
public static void main(String[] args) throws IOException {
// 读取文件,文件必须存在
FileInputStream fis = new FileInputStream("D:/1.txt");
byte[] buffer = new byte[1024];
int offset = 0;
int length = 1024;
int maxlength = fis.available();
length = Math.min(length, maxlength);
while (fis.read(buffer, offset, length) != -1) {
System.out.print(new String(buffer));
offset = offset + length;
length = maxlength - length;
if (length <= 0) {
break;
}
}
fis.close();
// 写入文件,文件必须存在
FileOutputStream fos = new FileOutputStream("D:/2.txt");
String content = "你好";
fos.write(content.getBytes());
fos.close();
}