我正在尝试将数组中的整数保存到文本文件中。在使用我的main方法时,这两个方法似乎都没有起到作用,我想知道是否有人能指出我的错误。
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();发布于 2015-12-06 03:27:13
而是目录的问题
看看这个: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);这在我的pc上运行得很好:
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:/Users似乎受到系统的保护
要写入用户目录,请参阅以下内容: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);发布于 2015-12-06 03:30:36
因为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();
}
}
}您需要在path中使用双斜杠。link
https://stackoverflow.com/questions/34109627
复制相似问题