-4

//Reading the text file try{ File myFile = new File("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());
    }
chris
  • 11
  • 3
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – khelwood May 12 '16 at 08:50
  • Hi Chris, Welcome to SO. could you edit your post to show the error you get? – J. Chomel May 12 '16 at 08:53

1 Answers1

0

Try doing this. Your outputstream is not writing to anything.

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());
            }

    }

}
Chit Khine
  • 830
  • 1
  • 13
  • 34