请问我是编程的初学者,在使用FileInputStream在java中读取文本文件后,我得到null

时间:2016-05-12 08:43:30

标签: java filereader

//读取文本文件     尝试{        文件myFile =新文件(" C:\ Users \ Dell \ Documents \ NetBeansProjects \ test.txt")。getAbsoluteFile();         FileInputStream in = new FileInputStream(myFile);         OutputStream out = connection.getOutputStream();

    int bytes = 0;
    byte[] buffer = new byte[8192];
    int len;

    while ((len = in.read(buffer)) > 0) {
    out.write(buffer, 0, len);
    bytes += len;
}

System.out.println("Transfer completed, " + bytes + " bytes sent");

out.flush();
connection.close();


    }catch(Exception e){
    System.out.println(e.getMessage());
    }

1 个答案:

答案 0 :(得分:0)

尝试这样做。你的输出流没有写入任何内容。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class stack2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{

                File myFile = new File("D:\\light.txt").getAbsoluteFile();
                FileInputStream in = new FileInputStream(myFile);
                OutputStream out = new FileOutputStream("test.txt");

                int bytes = 0;
                byte[] buffer = new byte[8192];
                int len;

                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                    bytes += len;
                }

                System.out.println("Transfer completed, " + bytes + " bytes sent");

                out.flush();



            }catch(Exception e){
                System.out.println(e.getMessage());
            }

    }

}