无法写入文件内容

发布于 2025-01-02 16:02:03 字数 1462 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2025-01-09 16:02:03
PrintWriter out = new PrintWriter(new FileWriter("file_name"));

您传递的字符串表示:“file_name”,
尝试这样做:

PrintWriter out = new PrintWriter(new FileWriter(new File(file_name)));

我认为它应该有效。我认为,由于 FileWriter 构造函数应该以 File 作为参数。

PrintWriter out = new PrintWriter(new FileWriter("file_name"));

You're passing a String that says: "file_name",
try doing that:

PrintWriter out = new PrintWriter(new FileWriter(new File(file_name)));

I think it should work. As FileWriter constructor should take File as a parameter, I think.

凯凯我们等你回来 2025-01-09 16:02:03

file_name 周围不能有引号。如果将它们放在一起,它会被解释为字符串,并且数据将写入工作目录中名为“file_name”的文件中。

PrintWriter out = new PrintWriter(
new FileWriter(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.

PrintWriter out = new PrintWriter(
new FileWriter(file_name));

...would be correct

情深缘浅 2025-01-09 16:02:03

最好的方法就是让它变得简单:尝试下面的代码:

fos=new File(filepath);
if(fos.exists() && fos.isFile() && !fos.isDirectory())
{
    FileWriter fw=new FileWriter(fos);
    BufferedWriter Bw= new BufferedWriter(fw);
    Bw.append("i am appending text to the existing file");
}

如果您想通过创建新文件进行写入,则首先进行文件搜索,如果不存在,则使用以下方法创建并写入数据:

fos=new File(filepath);
if(!fos.exists() && !fos.isFile())
{
    FileWriter fw=new FileWriter(fos);  
    BufferedWriter Bw= new BufferedWriter(fw);
    Bw.write("i am writing text to the existing file");
}

Best way is just make it simple: try below code :

fos=new File(filepath);
if(fos.exists() && fos.isFile() && !fos.isDirectory())
{
    FileWriter fw=new FileWriter(fos);
    BufferedWriter Bw= new BufferedWriter(fw);
    Bw.append("i am appending text to the existing file");
}

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:

fos=new File(filepath);
if(!fos.exists() && !fos.isFile())
{
    FileWriter fw=new FileWriter(fos);  
    BufferedWriter Bw= new BufferedWriter(fw);
    Bw.write("i am writing text to the existing file");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文