java输入/输出文件问题

时间:2015-06-10 15:15:51

标签: java

这个方法的目的是创建一个文件,如果它不存在但是如果存在:打开它并修改它...... 但是我得到错误createNewFile()不是类的成员,exists()不是类的成员..我已经导入了java.io. * 现在我发布了一个片段..我将通过评论高亮显示问题以获得更快的帮助...

  public void writeCoordtoFile () throws IOException
  {
       File file = new File("FermiPresentCoord.txt");
     //  boolean yes = createNewFile("FermiPresentCoord.txt") throws IOException; i get error when i try to do this
       //boolean yes = exists("FermiPresentCoord.txt"); error exists is not a member of file 
       // creates the file
       file.createNewFile();

       // creates a FileWriter Object
       FileWriter writer = new FileWriter(file);

       // Writes the content to the file
       writer.write(pos_Let);
       writer.write(pos_Num);
       writer.flush();
       writer.close();

  }

2 个答案:

答案 0 :(得分:0)

您需要提供file对象,因为createNewFile()File类的方法。可以使用文件object(file).methodName()

访问该班级的功能
File file = new File("FermiPresentCoord.txt");
boolean yes = file.createNewFile() 
boolean yes1 = file.exists(); 

答案 1 :(得分:0)

public static void main(String[] args) throws IOException {
        File file = new File("F:\\FermiPresentCoord.txt");

        file.createNewFile();

        FileWriter writer = new FileWriter(file, true);

        writer.write("x");
        writer.flush();
        writer.close();
    }

同样的错误? 如果你没有指定相对于你的java执行的路径,请尝试更改“F:\”。如果这是您的系统分区,您可以获得权限创建文件的问题。