字节型

FileInputStream读取

  1. package test;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.*;
  7. public class Test {
  8. public static void main(String[] args) {
  9. FileInputStream stream = null;
  10. try {
  11. stream = new FileInputStream(new File("./abc.txt"));
  12. byte[] value = new byte[5]; // 利用byte[]的方式读取
  13. int count = stream.read(value);
  14. while (count != -1){
  15. System.out.print(new String(value,0,count));
  16. count = stream.read(value);
  17. }
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21. try {
  22. if (stream != null) {
  23. stream.close();
  24. }
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. // File file = new File("./abc.txt");
  30. // try {
  31. // FileInputStream stream = new FileInputStream(file);
  32. // int i = stream.read();
  33. // stream.available(); // 有多少字节可用
  34. //
  35. // //byte[] value = new byte[5];
  36. // //stream.read(value); // 带byte[]参数的读取文件
  37. //
  38. // stream.skip(3); // 跳过几个字节
  39. // while (i != -1) {
  40. // System.out.print((char) i);
  41. // i = stream.read(); // 读取文件
  42. // }
  43. // stream.close(); // 关闭流通道,必须关闭
  44. // } catch (IOException e) {
  45. // e.printStackTrace();
  46. // }
  47. }
  48. }

FileOutputStream写入

  1. package test;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Test {
  5. public static void main(String[] args) {
  6. String str = "abcd";
  7. FileOutputStream fos = null;
  8. try {
  9. // 如果没有这个文件就会自己创建
  10. fos = new FileOutputStream(new File("./abc.txt"), true); // 用追加的方式写入
  11. byte[] value = str.getBytes(); // 用byte[]的方法写 ,可以写入字节
  12. fos.write(value); // 写入
  13. fos.flush(); // 刷新
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. } finally {
  17. try {
  18. if (fos != null) {
  19. fos.close();
  20. }
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

字符型

FileRead读取 只能渡纯文本

  1. package test;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Test {
  5. public static void main(String[] args) {
  6. File file = new File("./abc.txt");
  7. FileReader reader = null;
  8. try {
  9. reader = new FileReader(file);
  10. //int code = reader.read(); // 读取单个字符
  11. char[] chars = new char[1024];
  12. int count = reader.read(chars); // 使用char数组
  13. while (count != -1) {
  14. System.out.print(new String(chars,0 ,count));
  15. count = reader.read(chars);
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. try {
  21. if (reader != null) {
  22. reader.close();
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }

FileWirte写入

  1. package test;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Test {
  5. public static void main(String[] args) {
  6. File file = new File("./abc.txt");
  7. FileReader reader = null;
  8. FileWriter writer = null;
  9. try {
  10. writer = new FileWriter(file,true);
  11. String str = "我擦撒娇撒";
  12. writer.write(str.toCharArray()); // char数组写入
  13. writer.write('a');
  14. writer.write(100); // char类型写入
  15. writer.write("我"); // 字符串类型写入
  16. writer.flush();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. try {
  21. if (writer != null) {
  22. writer.close();
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }