无法写入文件内容
package employee;
import employee.nidhin.staples;
import java.util.*;
import java.io.*;
public class Employee {
public static void main(String[] args)
{
int j=3;
staples[] stemp = new staples[j];
String file_name = "d:/personal/11636470/NetBeansProjects/Employee/src/employee/Xanadu.txt";
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for ( j=0;j<3;j++)
{
stemp[j] = new staples();
System.out.print("Enter your name : ");
stemp[j].setName(reader.readLine());
System.out.println("Enter your age : ");
stemp[j].setAge(Integer.parseInt(reader.readLine()));
}
for ( j=0;j<3;j++)
{
System.out.println("Employee number:" + j +" name:"+stemp[j].getName()+" Age:"+stemp[j].getAge() );
}
reader.close(); // VERY IMPORTANT TO CLOSE
System.out.println("Now writing the file to Xanadu.txt ");
PrintWriter out = new PrintWriter(
new FileWriter("file_name"));
for (int i = 0; i < 3; i++)
{
out.println("Value at: "+ i + " = "+ stemp[i].getName());
}
System.out.println("Successfully wrote to file");
out.close();
}
catch(java.io.IOException ex)
{
System.out.println("Error is " + ex.getMessage() );
}
}
}
程序执行成功,但是当我打开输出文件 Xanadu.txt 时,我什么也没看到。有人可以指导我吗?文件 Xanadu.txt 的内容是一个对象 stemp 的数组,它有两个属性 name 和age。
package employee;
import employee.nidhin.staples;
import java.util.*;
import java.io.*;
public class Employee {
public static void main(String[] args)
{
int j=3;
staples[] stemp = new staples[j];
String file_name = "d:/personal/11636470/NetBeansProjects/Employee/src/employee/Xanadu.txt";
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for ( j=0;j<3;j++)
{
stemp[j] = new staples();
System.out.print("Enter your name : ");
stemp[j].setName(reader.readLine());
System.out.println("Enter your age : ");
stemp[j].setAge(Integer.parseInt(reader.readLine()));
}
for ( j=0;j<3;j++)
{
System.out.println("Employee number:" + j +" name:"+stemp[j].getName()+" Age:"+stemp[j].getAge() );
}
reader.close(); // VERY IMPORTANT TO CLOSE
System.out.println("Now writing the file to Xanadu.txt ");
PrintWriter out = new PrintWriter(
new FileWriter("file_name"));
for (int i = 0; i < 3; i++)
{
out.println("Value at: "+ i + " = "+ stemp[i].getName());
}
System.out.println("Successfully wrote to file");
out.close();
}
catch(java.io.IOException ex)
{
System.out.println("Error is " + ex.getMessage() );
}
}
}
The program execute successfully , but when I open the outputfile Xanadu.txt , I see nothing. Can someone guide me? The contents of the file Xanadu.txt is an array of objects stemp which have two attributes name and age.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您传递的字符串表示:“file_name”,
尝试这样做:
我认为它应该有效。我认为,由于 FileWriter 构造函数应该以 File 作为参数。
You're passing a String that says: "file_name",
try doing that:
I think it should work. As FileWriter constructor should take File as a parameter, I think.
file_name
周围不能有引号。如果将它们放在一起,它会被解释为字符串,并且数据将写入工作目录中名为“file_name”的文件中。...是正确的
There must not be quotation marks around
file_name
. If you put them around, it is interpreted as a string, and the data is written to a file called "file_name" in the working dir....would be correct
最好的方法就是让它变得简单:尝试下面的代码:
如果您想通过创建新文件进行写入,则首先进行文件搜索,如果不存在,则使用以下方法创建并写入数据:
Best way is just make it simple: try below code :
if you want to write by creating new file then go for file searching first and if does not exist then create and write data in same using below: