• java io读取数据


    1.字节流读取数据 

    2.字节流读取数据: read()

    1. package wwx;
    2. import jdk.swing.interop.SwingInterOpUtils;
    3. import java.io.*;
    4. import java.nio.charset.StandardCharsets;
    5. public class Test {
    6. public static void main(String[] args) {
    7. FileInputStream fileInputStream = null;
    8. try {
    9. fileInputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");
    10. } catch (FileNotFoundException e) {
    11. e.printStackTrace();
    12. }
    13. try {
    14. int read = fileInputStream.read();
    15. System.out.println((char)read);
    16. int read1 = fileInputStream.read();
    17. System.out.println((char)read1);
    18. int read2 = fileInputStream.read();
    19. System.out.println((char)read2);
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. finally {
    24. try {
    25. fileInputStream.close();
    26. } catch (IOException e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. }
    31. }

     

     优化:

    1. package wwx;
    2. import jdk.swing.interop.SwingInterOpUtils;
    3. import java.io.*;
    4. import java.nio.charset.StandardCharsets;
    5. public class Test {
    6. public static void main(String[] args) {
    7. FileInputStream fileInputStream = null;
    8. try {
    9. fileInputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");
    10. } catch (FileNotFoundException e) {
    11. e.printStackTrace();
    12. }
    13. try {
    14. int read ;
    15. // System.out.println((char)read);
    16. // int read1 = fileInputStream.read();
    17. // System.out.println((char)read1);
    18. // int read2 = fileInputStream.read();
    19. // System.out.println((char)read2);
    20. // while (read != -1) {
    21. // System.out.println((char) read);
    22. // read = fileInputStream.read();
    23. // }
    24. //优化
    25. while ((read = fileInputStream.read()) != -1) {
    26. System.out.print((char) read);
    27. }
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. } finally {
    31. try {
    32. fileInputStream.close();
    33. } catch (IOException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37. }
    38. }

    3.字节流复制文件:

    1. package wwx;
    2. import jdk.swing.interop.SwingInterOpUtils;
    3. import java.io.*;
    4. import java.nio.charset.StandardCharsets;
    5. public class Test {
    6. public static void main(String[] args) throws IOException {
    7. FileInputStream fileInputStream = new FileInputStream("E:\\Test\\wwx.txt");
    8. FileOutputStream fileOutputStream = new FileOutputStream("E:\\cTest\\wwx.txt");
    9. int by;
    10. while ((by = fileInputStream.read()) != -1) {
    11. fileOutputStream.write(by);
    12. }
    13. fileInputStream.close();
    14. fileOutputStream.close();
    15. }
    16. }

     4.byte数组转为字符串:new String(byte)方法

    1. package wwx;
    2. public class Test {
    3. public static void main(String[] args) {
    4. byte[] bytesArray={97,98,99};
    5. String s = new String(bytesArray);//用构造方法创建的,值为正常值
    6. System.out.println("byte数组转为字符串形式:"+s);
    7. String str1="3,4,5,67";
    8. byte[] bytes1=str1.getBytes();//该方法返回值为内存地址
    9. System.out.println("字符串转为byte数组"+bytes1);
    10. }
    11. }

     

    5.字节流以字节数组形式读取数据:

    idea读取该记事本的数据 

    该记事本f之后换行,换行为一个\n,占两个字节 

    1. package wwx;
    2. import jdk.swing.interop.SwingInterOpUtils;
    3. import java.io.*;
    4. import java.nio.charset.StandardCharsets;
    5. public class Test {
    6. public static void main(String[] args) throws IOException {
    7. FileInputStream input = new FileInputStream("E:\\wwx\\xx\\wx.txt");
    8. byte[] bytes = new byte[5];
    9. System.out.println("第1次读取数据");
    10. int bytesLen = input.read(bytes);
    11. System.out.println("byte数组长度:"+bytesLen);
    12. System.out.println(new String(bytes));
    13. System.out.println("第2次读取数据");
    14. bytesLen = input.read(bytes);
    15. System.out.println("byte数组长度:"+bytesLen);
    16. System.out.println(new String(bytes));
    17. System.out.println("第3次读取数据");
    18. bytesLen = input.read(bytes);
    19. System.out.println("byte数组长度:"+bytesLen);
    20. System.out.println(new String(bytes));
    21. input.close();
    22. }
    23. }

    6.要求一次性读取wx.txt 中13个字节数据 :

    1. package wwx;
    2. import jdk.swing.interop.SwingInterOpUtils;
    3. import java.io.*;
    4. import java.nio.charset.StandardCharsets;
    5. /*
    6. 要求一次性读取wx.txt 中13个字节数据
    7. */
    8. public class Test {
    9. public static void main(String[] args) throws IOException {
    10. FileInputStream inputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");
    11. byte[] bytes = new byte[13];
    12. int len = inputStream.read(bytes);
    13. System.out.println("byte数组字节长度:"+len);
    14. System.out.println(new String(bytes));
    15. inputStream.close();
    16. }
    17. }

    7.字节流以1024字节读取数据:

     

    1. package wwx;
    2. import java.io.*;
    3. /*
    4. 要求一次性读取wx.txt 中13个字节数据
    5. */
    6. public class Test {
    7. public static void main(String[] args) throws IOException {
    8. FileInputStream inputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");
    9. byte[] bytes = new byte[1024];
    10. int len;
    11. while ((len = inputStream.read(bytes)) != -1) {
    12. System.out.println("byte数组字节长度:" + len);
    13. System.out.println(new String(bytes,0,len));
    14. }
    15. inputStream.close();
    16. }
    17. }

  • 相关阅读:
    神经网络 深度神经网络,深度神经网络知识图谱
    Python整理本机Mp3音乐文件
    Apache Calcite 动态数据管理框架整合 csv 实战笔记
    Kafka 知识点分析
    PF-Net基于深度学习的点云补全网络
    威纶通触摸屏如何编写和调用宏指令进行逻辑判断
    视频基础知识
    Pytest系列-失败重跑插件pytest-rerunfailures的使用(9)
    【毕业设计】1-1Matlab小电流接地系统的建模与单相故障的仿真分析(仿真工程文件+结果图+论文+PPT)
    OPPO的关键一步
  • 原文地址:https://blog.csdn.net/qq_64005599/article/details/133815516