0

I am trying to save the integers in an array to a text file. Neither of these seem to be doing the trick while sitting in my main method and I was wondering if someone could point out my mistake.

public static void main (String[] params) throws IOException
{ 
   numberPlayers();
   int diceroll = dicethrow(6);
   int[] scorep1 = scorearrayp1();
   questions(diceroll, scorep1);
   sort(scorep1);

   File file = new File ("C:/Users/Usman/Desktop/directory/scores.txt");
   PrintWriter writer = new PrintWriter("scores.txt");
   writer.println("Player 1 score: " + scorep1[0]);
   writer.println("Player 2 score: " + scorep1[1]);
   writer.println("Player 3 score: " + scorep1[2]);
   writer.println("Player 4 score: " + scorep1[3]);
   writer.close();

   System.exit(0);
}

No score.txt file is created on my desktop in either of these attempts.

public static void main (String[] params) throws IOException
{ 
   numberPlayers();
   int diceroll = dicethrow(6);
   int[] scorep1 = scorearrayp1();
   questions(diceroll, scorep1);
   sort(scorep1);

   File file = new File("C:/Users/Me/Desktop/file.txt");
   PrintWriter printWriter = null;
   try
   {
      printWriter = new PrintWriter(file);
      printWriter.println("hello");
   }
   catch (FileNotFoundException e)
   {
      e.printStackTrace();
   }
   finally
   {
     if ( printWriter != null ) 
     {
        printWriter.close();
     }
   }

   System.exit(0);
}

EDIT: This is what I have made of the answers so far, please feel free to edit the wrong bit so I can clearly see what I've missed.

System.out.println("What's happening");
   String path = System.getProperty("user.home") + File.separator + "Desktop/file1.txt";
   File file = new File(path);

   PrintWriter printWriter = null;
   try
   {
      printWriter = new PrintWriter(file);
      printWriter.println("hello");
      FileWriter fileWriter = new FileWriter(file);
   }
   catch (FileNotFoundException e)
   {
      e.printStackTrace();
   }
   finally
   {
     if ( printWriter != null ) 
     {
        printWriter.close();
     }
   }

Also what about this:

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
Overclock
  • 73
  • 7

2 Answers2

1

Rather a problem of directory

see this: How to use PrintWriter and File classes in Java?

If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.

 // NOK for C:/Users   see below
 // File file = new File("C:/Users/Me/Desktop/file.txt");
 File file = new File("C:/classical_dir/file.txt");
 file.getParentFile().mkdirs();

 PrintWriter printWriter = new PrintWriter(file);

This works well on my pc:

 File file = new File("C:/foo/bar/blurps/file.txt");

This throws an exception: windows seems not to want it

 File file = new File("C:/Users/Me/Desktop/file.txt");

because, C:/Users/Me seems to be prohibited: C:/Users seems to be protected by system

see this for writing in User directory: how can I create a file in the current user's home directory using Java?

    String path = System.getProperty("user.home") + File.separator + "Desktop/file1.txt";
    File file = new File(path);

see this also: How to get the Desktop path in java

Community
  • 1
  • 1
0

Because File object does not create a physical copy of a file. For details follow this link
Does creating a File object create a physical file or touch anything outside the JVM?

Now if we come to your solution then first make a empty file on the disk by following command

     import java.io.*;

public class Test {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "C:\\Users\\MOSSAD\\Desktop\\new\\temp.txt";

        try {
            // Assume default encoding.
            FileWriter fileWriter =
                new FileWriter(fileName);

            File file = new File (fileName);
   PrintWriter writer = new PrintWriter(file);
   writer.println("Player 1 score: " + 5);
   writer.println("Player 2 score: " + 2);
   writer.println("Player 3 score: " + 3);
   writer.println("Player 4 score: " + 4);
   writer.close();
        }
        catch(IOException ex) {
            System.out.println(
                "Error writing to file '"
                + fileName + "'");
            // Or we could just do this:
            // ex.printStackTrace();
        }
    }
}

you need to use double slash in path .link

Community
  • 1
  • 1
Shvet Chakra
  • 1,043
  • 10
  • 21
  • I *think* I follow what you're trying to say, but I'm not too sure. I have added an edit to my question for you to check if I've made the changes you think are needed. If I haven't, could you please edit it so that I may see what you meant. Thank you – Overclock Dec 05 '15 at 19:45
  • I have edited my answer you can follow here what i was trying to explain before. – Shvet Chakra Dec 05 '15 at 19:54
  • This is not working either. Is there some other major problem as to where I've placed the code or something like that? It's ridculous how I can try every possible alteration and still come up without getting the file saved. – Overclock Dec 05 '15 at 20:05
  • Unfortunately it still doesn't work. I'm not getting any errors back from the complier so that tells me the code isn't the problem but there isn't a file anywhere to be found on my desktop. – Overclock Dec 05 '15 at 21:17
  • have you put the right path like : C:\\Users\\Usman\\Desktop\\directory\\scores.txt – Shvet Chakra Dec 05 '15 at 21:20
  • I have. Where you have `File file = new File (fileName);` is `fileName` supposed to be scores? Other than this I can't think of anything else – Overclock Dec 05 '15 at 21:23
  • No that should be in that way. – Shvet Chakra Dec 05 '15 at 21:24
  • I've left it as that and it's still not working. Thank you very much for your help regardless, maybe it's something that I'll end up stumbling across in my own time. – Overclock Dec 05 '15 at 21:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97089/discussion-between-shvet-chakra-and-overclock). – Shvet Chakra Dec 05 '15 at 21:30