使用FileInputStream读取文件
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public static void main(String[] args) { File dir = new File("/tmp/Temp/temp"); File file = new File("/tmp/Temp/temp/test.txt"); if (!dir.exists()) { dir.mkdir();//新建目录 } if (!file.exists()) { try { file.createNewFile();//新建文件 } catch (IOException e) { e.printStackTrace(); } } //读取文件 try { FileInputStream fileInputStream = new FileInputStream(file); int num = fileInputStream.read();//一次读取一个字节 System.out.println(num); int contentLength; byte[] bytes= new byte[1024]; String content=null; //超过1kb while ((contentLength = fileInputStream.read(bytes))!=-1){ content=new String(bytes,0,contentLength);//转化成字符串 } System.out.println(content); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
使用FileOutputStream写文件
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class FileWriteDemo { public static void main(String[] args) { try { FileOutputStream fileOutputStream = new FileOutputStream("/tmp/Temp/temp/good.txt"); String msg="weekend"; byte[] bytes=msg.getBytes(StandardCharsets.UTF_8); fileOutputStream.write(bytes,0,bytes.length);//写文件 fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
使用BufferedReader和FileReader读取文件
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class BufferReaderDemo { public static void main(String[] args) { try { BufferedReader bufferedReader = new BufferedReader(new FileReader("/tmp/Temp/temp/test.txt")); // int read = bufferedReader.read();//再次读取有可能丢失首字母 // System.out.println(read); //按行读取 // String readLine = null; // while ((readLine = bufferedReader.readLine()) != null) { // System.out.println(readLine); // } //读取十个字符 // char[] chars = new char[10]; // int length = bufferedReader.read(chars, 0, chars.length); // System.out.println(length); // System.out.println(new String(chars,0,length)); //超过十个字符 // char[] chars = new char[10]; // int length=0; // while ((length=bufferedReader.read(chars,0,chars.length))!=-1){ // System.out.println(new String(chars,0,length)); // } bufferedReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
使用BufferedWriter和FileWriter写文件
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferWriterDemo { public static void main(String[] args) { try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/tmp/Temp/temp/new.txt")); bufferedWriter.write("first line"); bufferedWriter.newLine();//换行 bufferedWriter.write("second line"); bufferedWriter.newLine(); bufferedWriter.flush();//清楚空余 bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
使用ObjectOutStream序列化和ObjectInputSstream反序列化
import java.io.*; public class ObjectDemo { public static void main(String[] args) { // Student student = new Student("jack",20); try { //对象的序列化 // FileOutputStream fileOutputStream = new FileOutputStream("/tmp/Temp/temp/student.bin"); // ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); // // objectOutputStream.writeObject(student); // objectOutputStream.close(); // fileOutputStream.close(); //反序列化 FileInputStream fileInputStream = new FileInputStream("/tmp/Temp/temp/student.bin"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Student student = (Student) objectInputStream.readObject(); System.out.println(student); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/275256.html