1.IO流
2. 1字节流(一切皆为字节)
字节输出流
public static void main(String[] args) throws IOException{
FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\a.txt");
fos.write(97);
fos.write(97);
byte[] b1= {65,66,65,67};
fos.write(b1);
fos.close();
FileOutputStream fos1=new FileOutputStream(new File("E:\\javacode\\111\\src\\io\\b.txt"));
fos1.write(97);
fos1.write(97);
fos1.write(b1, 1, 2);
byte[] b2="你好".getBytes();
System.out.println(Arrays.toString(b2));
fos1.write(b2);
fos1.close();
}
FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\a.txt", true);
for (int i = 0; i < 3; i++) {
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
字节输入流
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\javacode\\111\\src\\io\\b.txt");
int a=0;
while ((a=fis.read())!=-1) {
System.out.println(a);
}
fis.close();
}
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\javacode\\111\\src\\io\\b.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();
}
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\aa.txt");
FileInputStream fis = new FileInputStream("C:\\Users\\THINK\\Desktop\\ps.txt");
byte[] bytes =new byte[1024];
int len=0;
while ((len=fis.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
2.2 字符流
字符流读取文件
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("E:\\javacode\\111\\src\\io\\a.txt");
char[] bytes = new char[1024];
int len = 0;
while ((len = fr.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
fr.close();
}
字符流写入数据
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("E:\\javacode\\111\\src\\io\\d.txt");
fw.write(99);
char[] b= {'a','2','3','-','-'};
fw.write(b);
fw.write("你好!");
fw.write("你好!",2,1);
fw.flush();
fw.close();
}
**
属性集
Properties类表示一个持久层的属性集。Properties可保存在流中或从流中加载
Properties集合是一个唯一和IO流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
可以使用Properties集合中load,把硬盘中保存的文件(键值对),读到集合中使用
属性列表中每个键及其对应值都是一个字符串
Properties集合是一个双列集合,key和value默认都是字符串