赞
踩
- package com.lu.day11.test;
-
- import java.io.FileReader;
- import java.io.IOException;
-
- public class Test {
- public static void main(String[] args) {
- try(FileReader fileReader = new FileReader("resource/b.txt");) {
-
- char[] chars = new char[1024];
- int read;
- while ((read = fileReader.read(chars)) != -1) {
- System.out.println(new String(chars, 0, read));
-
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileWriter;
- import java.io.IOException;
-
- public class Test1 {
- public static void main(String[] args) {
- try (FileWriter fileWriter = new FileWriter("resource/a.txt");){
-
- fileWriter.write("hello");
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
-
- public class Test {
- public static void main(String[] args) {
- try(BufferedReader bufferedReader = new BufferedReader(new FileReader("resource/b.txt"))) {
- //判断是否读取结束
- while (bufferedReader.ready()){
- //读取一行
- System.out.println(bufferedReader.readLine());
-
- //jdk8新特性
- //bufferedReader.lines().forEach(System.out::println);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
-
- public class Test1 {
- public static void main(String[] args) {
- try ( BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter("resource/a.txt",true))){
-
- bufferedWriter.write("hello world");
- //换行屏蔽不同操作系统差异性
- bufferedWriter.newLine();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
-
- public class Test2 {
- public static void main(String[] args) {
- try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
- out.writeObject(new Student("张三", 18));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.Serializable;
- //使用序列化要使用Serializable接口
- public class Student implements Serializable {
- private String name;
- private int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.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;
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.util.ArrayList;
-
- public class Test3 {
- public static void main(String[] args) {
- try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"))){
- ArrayList<Student> o = (ArrayList<Student>) in.readObject();
- System.out.println(o);
- } catch (IOException | ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
-
- public class Test2 {
- public static void main(String[] args) {
- try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
- ArrayList<Student> list = new ArrayList<>();
- list.add(new Student("李四", 19));
- out.writeObject(list);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileWriter;
- import java.util.Properties;
-
- public class Test4 {
- public static void main(String[] args) {
- Properties properties = new Properties();
- properties.setProperty("name", "lu");
- properties.setProperty("age", "18");
- try (FileWriter fileWriter = new FileWriter("resource/test.properties");){
- //comments 注释在文件里中文会保存为unicode编码
- //.properties
- properties.store(fileWriter, "这是练习");
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- package com.lu.day11.test;
-
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Properties;
-
- public class Test5 {
- public static void main(String[] args) {
- Properties properties = new Properties();
- try( FileReader fileReader = new FileReader("resource/test.properties");){
- properties.load(fileReader);
- properties.forEach((k,v)->System.out.println(k+"--->"+v));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。