文件夹和文件创建不起作用

时间:2017-01-19 04:37:01

标签: java runtime-error ioexception

我目前无法创建文件夹和文件(如果不存在)。我不断得到并且没有指定它的错误,我真的不知道如何解决它。

这是我的主要

import java.io.IOException;


public class main {

    /**
     * @param args the command line arguments
     */


    public static void main(String[] args) throws IOException{
        timeKeepHome ja = new timeKeepHome();
        //ja.setVisible(true);
        fileTimeLog log = new fileTimeLog();


        log.checkFile();

    }

}

这是我创建文件/文件夹的类。

import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Formatter;


public class fileTimeLog 
{
    File log = new File("log/sixWeek.dat");
    File folder = new File("log");
    PrintWriter logW = new PrintWriter("log/sixWeek.dat");

    public fileTimeLog() throws IOException
    {
        System.out.println("successful");
        checkFile();
    }

    public void checkFile() throws IOException
    {
        if(!(log.exists()))
        {
            createFile();

        }
    }
    public void saveTime() throws IOException
    {

    }

    public void saveDate()
    {

    }

    public void createFile() throws IOException
    {
        folder.mkdir();
        logW = new PrintWriter(log);
        logW.println("DONT MODIFY THIS FILE IF UNLESS YOU KNOW WHAT YOUR ARE DOING ");
        logW.close();

    }

}

和错误我正在

Exception in thread "main" java.io.FileNotFoundException: log\sixWeek.dat (The system cannot find the path specified)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at java.io.PrintWriter.<init>(PrintWriter.java:184)
    at fileTimeLog.<init>(fileTimeLog.java:17)
    at main.main(main.java:18)

我会让一切看起来更好更整洁,但我真的很沮丧,我做过研究,但不太懂。

1 个答案:

答案 0 :(得分:0)

const regex = /(.*?)\|(.*?)\|(.*?)\|(.*)/gm; const str = `Field A||Field C|Field D`; var m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }会尝试创建一个文件,所以当你这样做时

Printwriter

在您的课程定义中,然后它会尝试创建它,但在您执行 PrintWriter logW = new PrintWriter("log/sixWeek.dat"); 之前该文件夹不存在

无论如何,当您在createFile重新初始化logW时,您无需在课程定义中对其进行初始化