这是否正确写入文本文件?

时间:2015-12-05 19:03:04

标签: java arrays file-io

我试图将数组中的整数保存到文本文件中。在坐在我的主要方法中时,这些似乎都没有做到这一点,我想知道是否有人可以指出我的错误。

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

在这些尝试中,我的桌面上都没有创建score.txt文件。

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);
}
编辑:这是我到目前为止所做的答案,请随意编辑错误的位,这样我就可以清楚地看到我错过了什么。

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

还有这个:

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

2 个答案:

答案 0 :(得分:1)

而不是目录

的问题

看到这个: How to use PrintWriter and File classes in Java?

如果目录不存在,则需要创建目录。 Java不会自己创建它,因为File类只是一个实际上根本不存在的实体的链接。

 // 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);

这在我的电脑上效果很好:

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

这引发了一个异常:Windows似乎不想要它

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

因为,C:/ Users / Me似乎被禁止:C:/用户似乎受系统保护

在用户目录中查看此内容: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);

另见:How to get the Desktop path in java

答案 1 :(得分:0)

因为File对象不创建文件的物理副本。有关详细信息,请点击此链接
Does creating a File object create a physical file or touch anything outside the JVM?

现在,如果我们找到您的解决方案,那么首先按照命令

在磁盘上创建一个空文件
     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();
        }
    }
}

你需要在路径中使用双斜杠。link