1.IO流分类
字节流:
输入流:FileInputStream、FilterInputStream、ObjectInputStream、StringBufferInputStream
输出流:FileOutputStream、FilterOutputStream、ObjectOutputStream、StringBufferOutputStream
字符流:
输入流:InputStreamReader、StringReader、BufferedReader、FilterReader
输出流:InputStreamWriter、StringWriter、BufferedWriter、FilterWriter
2.使用
FileOutputStream
public class Demo01OutputSteam {
public static void main(String[] args) throws IOException {
FileOutputStream fos= new FileOutputStream("d:\\test\\abc.txt");
fos.write("你好".getBytes());
fos.write(97);
fos.close();
}
}
追加写入 append:true
public class Demo02OutputSteam {
public static void main(String[] args) throws IOException {
FileOutputStream fos= new FileOutputStream(new File("d:\\test\\a.txt"),true);
fos.write("\r\n".getBytes());
fos.write(49);
fos.write(48);
fos.write(48);
fos.write("\r\n".getBytes());
byte[] bytes = {65,66,67,68,69,78,70};
fos.write(bytes);
fos.close();
}
}
FileInputStream
public class Demo03InputSteam {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("d:\\test\\a.txt");
int len=0;
while((len=fis.read())!=-1){
System.out.print((char) len);
}
show01();
}
private static void show01() throws IOException {
FileInputStream fis = new FileInputStream("d:\\test\\a.txt");
byte[] bytes = new byte[1024];
int len =0;
while ((len=fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fis.close();
}
}
FileInputStream与FileOutputStream结合使用
private static void copyDoc() throws IOException {
File f = new File("d:\\test\\hetl");
if(!f.exists()){
f.mkdirs();
}
FileInputStream fis = new FileInputStream("d:\\test\\1.jpg");
FileOutputStream fos = new FileOutputStream(f+"\\1.jpg");
int len=0;
while((len = fis.read())!=-1){
//System.out.println(len);
fos.write(len);
}
fos.close();
fis.close();
}
BufferedInputStream、BufferedOutputStream
private static void show02() throws IOException {
FileInputStream fis = new FileInputStream("d:\\test\\a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len = 0;
byte[] bytes = new byte[1024];
while ((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
bis.close();
}
private static void show01() throws IOException {
FileOutputStream fos = new FileOutputStream("d:\\test\\b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("欢迎观看撒v是".getBytes());
bos.flush();
bos.close();
}
FileReader:
public class Demo05Reader {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("d:\\test\\a.txt");
char[] chars = new char[1024];
int len = 0;
while ((len = fr.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
fr.close();
}
private static void show01() throws IOException {
Properties prop = new Properties();
prop.load(new FileReader("d:\\test\\prop.properties"));
Set<String> set=prop.stringPropertyNames();
for (String s:set){
System.out.println(prop.getProperty(s));
}
}
}
FileWriter:
public class Demo06Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d:\\test\\a.txt",true);
fw.write("\r\n");
fw.write("我是啦啦啦");
fw.flush();
fw.close();
}
}
OutputStreamWriter
public class Demo01OutputStreamWriter {
public static void main(String[] args) throws IOException {
writer_utf_8();
}
private static void writer_utf_8() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\test\\c.txt"),"utf-8");
osw.write("你好");
osw.flush();
osw.close();
}
}
ObjectInputStream与ObjectOutputStream
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Demo01ObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
show01();
show02();
}
private static void show02() throws IOException, ClassNotFoundException {
ObjectInputStream oos = new ObjectInputStream(new FileInputStream("d:\\test\\person.txt"));
Object o = oos.readObject();
oos.close();
System.out.println(o);
}
private static void show01() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\test\\person.txt"));
oos.writeObject(new Person("zhangsan",18));
oos.close();
}
}
版权声明:本文为m0_46540089原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。