package com.cn6;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\\fos.txt");
fos.write("hello".getBytes());
// fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
fos.close();
}
}
}
从文件读取数据:1、创建FIS对象 2、定义一个by 3、while循环使得(by=fio.read)!=-1 {} 4、sout 5、记得要fis.close();
package com.cn6;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
import java.util.function.DoubleToIntFunction;
public class FileOutDemo {
public static void main(String[] args) throws IOException{
// FileImageOutputStream fio = new FileImageOutputStream(new File("E://fos.txt"));
FileInputStream fis = new FileInputStream("D:\\fos.txt");
// int by = fio.read();
// System.out.println(by);
// System.out.println((char) by);
//
//
// by = fio.read();
// System.out.println(by);
// System.out.println((char) by);
int by;
while ((by=fis.read())!=-1){
System.out.print((char)by);
// by=fio.read();
}
fis.close();
}
}
方式1:
2: