java 文件操作 读取txt文本(兄弟常开心)

测试一下读取文本的另一种方法:该方法只使用一个类读取了文件

注意:buffer和read方法中读取指定长度的一致

package com.swust;
import java.io.*;
/*
 * 数据流包括DataInputStream、DataOutputStream类,
 * 两个类的实例化完成流的操作
 */
public class flowTest {
 
    public static void main(String[] args) {
        byte buffer[]=new byte[2048];
        // TODO Auto-generated method stub
          
          String path = "C:/test/1/" ;
          String inforTxt = "1.txt";
          String newPath=path+inforTxt;
          
          try{
              FileInputStream fileInput = new FileInputStream(newPath);
              int bytes=fileInput.read(buffer, 0, 2048);
              String str=new String(buffer,0,bytes);
              System.out.println(str);
              fileInput.close();
          }catch(Exception e){
              System.out.println(e.toString());
          }
          
    }
    

}

读取效果:

你从国外回来咱就没怎么聊过
一直都在忙着其实有好多话想说
还没来得及转一眼又要回去了
兄弟
你这一走又是几年还真舍不得
都是大老爷们
说的有点女人了
又要离开了
你得惦记咱这哥几个
我好面儿
这些话太啰嗦有点说不出口
所以把它写成一首祝福的歌

原文地址:https://www.cnblogs.com/shuqingstudy/p/4728623.html