Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想创建一个类来创建一个文件,并使用main类来检查该文件是否已创建,但我的代码失败了。(爪哇)_Java_File - Fatal编程技术网

Java 我想创建一个类来创建一个文件,并使用main类来检查该文件是否已创建,但我的代码失败了。(爪哇)

Java 我想创建一个类来创建一个文件,并使用main类来检查该文件是否已创建,但我的代码失败了。(爪哇),java,file,Java,File,这是代码。我想用我创建的类来创建一个文件,写一些东西。然后,我使用main类检查该文件是否存在 但是,我不知道为什么,但是主类不识别我(可能)创建的文件。谁能告诉我如何把它们联系起来 我知道这个程序可能有一些小错误。我稍后会解决这个问题 谢谢 您从未调用过构造函数 public class Fileverifynanoha { private File fileext; private Path filepath; public Fileverifynanoha()//

这是代码。我想用我创建的类来创建一个文件,写一些东西。然后,我使用main类检查该文件是否存在

但是,我不知道为什么,但是主类不识别我(可能)创建的文件。谁能告诉我如何把它们联系起来

我知道这个程序可能有一些小错误。我稍后会解决这个问题


谢谢

您从未调用过构造函数

public class Fileverifynanoha 
{
    private File fileext;
    private Path filepath;
    public Fileverifynanoha()//this class wants to create a file, write something, and close it.
{
    filepath = Paths.get("./txttest.txt");
    Charset charset = Charset.forName("US-ASCII");
    String s = "Takamachi Nanoha. Shirasaki Tsugumi.!";
    try (BufferedWriter filewriter = Files.newBufferedWriter(filepath,charset))
    {
        filewriter.write(s,0,s.length()-1);   
    }
    catch(IOException e)
         {
            System.err.println(e);
         }

}//end of this class
/**
 * @param args the command line arguments
 */
public static void main(String[] args)//the main method will check if this file contains(created), if so, return exist. if not, return doesnt exist.
{

    if (filetxt.exists()&&!filetxt.isDirectory())//object does not create any real thing, therefore nothing true will return.
    {
        System.out.println("File exist.");
    }
    else
    {
        System.out.println("File does not exist.");
    }
}
}
您的问题:

  • 未创建类的实例
  • 没有初始化
    文件,因此它将始终为空
  • 最好对纯文本文件使用
    utf-8
试试这个:

public static void main(String[] args)//the main method will check if this file contains(created), if so, return exist. if not, return doesnt exist.
{
    Fileverifynanoha fvn = new Fileverifynanoha();
    if (fvn.filetxt.exists()&&!fvn.filetxt.isDirectory())
    {
        System.out.println("File exist.");
    }
    else
    {
        System.out.println("File does not exist.");
    }
}
}

您没有在这里实例化该类,您是在其他地方进行的吗?何时以及如何调用
Fileverifynanoha()
方法?另外,请关闭
filewriter
。我不确定这是打字错误还是什么。。filetxt和fileext应该是相同的东西吗?如果它们没有被初始化,那么它们都没有被初始化。如果可能的话,我想在这里创建一个对象。但是我不知道为什么我没有创建一个对象。哦,对不起。这是个打字错误。fileext是我想要使用的,filetxt是一个打字错误。他需要解决的不仅仅是这个问题。filetxt也未在类中初始化。抱歉。filetxt是一个输入错误。fileext是我真正想要使用的东西。谢谢你的代码!我想我可能知道如何使用创建的类在另一个类中执行某些操作的基本知识。有一本书叫做
4th
,它将帮助您进行
Java
&
面向对象的编程。如果您更喜欢使用
Java8
,那么
9th-volume1
将更为最新。这两本书都很棒。
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Fileverifynanoha {
    private File file;
    private Path path;

    public Fileverifynanoha(String fp) {
        this.path = Paths.get(fp);
        this.file = path.toFile();
    }

    public void createFile()// this class wants to create a file, write something, and close it.
    {
        Charset charset = Charset.forName("UTF-8");
        String s = "Takamachi Nanoha. Shirasaki Tsugumi.!";
        BufferedWriter filewriter = null;
        try {
            filewriter = Files.newBufferedWriter(path, charset);
            filewriter.write(s, 0, s.length() - 1);
            filewriter.close();
        } catch (IOException e) {
            System.err.println(e);
        }

    }// end of this class

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args)// the main method will check if this file contains(created), if so, return exist. if not, return doesnt exist.
    {
        Fileverifynanoha f = new Fileverifynanoha("./txttest.txt");
        f.createFile();

        if (f.file.exists() && !f.file.isDirectory())// object does not create any real thing, therefore nothing true will return.
        {
            System.out.println("File exist.");
        } else {
            System.out.println("File does not exist.");
        }
    }
}