-2

I am new to java and moving my codes since last two years from VB to java using netbeans 8 . Now i want to write loop acumalted data to file and last to save produced file to specific place using FlieChooser below is my code but i can't see any file in My Desktop when I wrote name in doilog and press enter :

public void   SaveToFile() throws IOException {
 try (Writer writer = new BufferedWriter(new OutputStreamWriter(
          new FileOutputStream("test.txt"), "utf-8"))) {
     int i=0;
     String Data[]=new String[10];
   while( i<10 ){
 writer.write("Student No :" + i);
  Data[i]= "Student No :" + i;
  ++i;
    }



   }

        int userSelection = db.showSaveDialog(this);
     if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = db.getCurrentDirectory();
                    String path = fileToSave.getAbsolutePath();
                    path = path.replace("\\", File.separator);
                    System.out.println("Save as file: " + path);

    }
    }
Mohamed Bawaneen
  • 139
  • 1
  • 12
  • to whom used to down vote any questions seems to them easy or essential , it's not making sense ,you should at lease along with your down-vote to indicate if the question was reapted or at least if not in order to mention the causes of the vote . there were a tendency among them I ntoiced to not answer complicated questions and only down vote anything looks easy to them this what I sensed with many quetions unattende ,that's why i used to rear ask here afew more google I can get better help! – Mohamed Bawaneen Nov 03 '16 at 17:35

1 Answers1

3

I see a couple problems with this. One, none of the code you're displaying here shows a call to any ".Save()" or copy or move method after choosing the directory. Two, the File object is pointing at a directory, not a file name. Three, your initial Writer object is probably writing test.txt to the directory your .class or .jar file lives in while it's running.

You need to figure out the directory and file name you want to use BEFORE you start writing to the disk.

UPDATE

public void   SaveToFile() throws IOException {
    int userSelection = db.showSaveDialog(this);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = db.getCurrentDirectory();
        String path = fileToSave.getAbsolutePath() + File.separator + "test.txt";

        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                  new FileOutputStream(path), "utf-8"))) {
            int i=0;
            //String Data[]=new String[10];
            while( i<10 ){
                writer.write("Student No :" + i);
                //Data[i]= "Student No :" + i; // Not sure why Data[] exists?
                ++i;
            }
        }

        System.out.println("Save as file: " + path);
    }
}

I think this is approximate to what you will need. I don't have a java compiler at the moment, so I can't say for sure if that's all good syntax. But there are plenty of Java tutorials online.

Matt
  • 1,213
  • 14
  • 39
  • can you lead me how to load data inside the string "Data" into a file and then how to save it suing bove FileChooser named "db" – Mohamed Bawaneen Nov 03 '16 at 17:12
  • it looks to me you are on the right track you just need to change up the order a bit. I'm not 100% keen on Java but the programming principles are basically the same. – Matt Nov 03 '16 at 17:14