如何创建一个文件夹以进一步创建.csv文件?

时间:2017-08-26 00:55:58

标签: java eclipse

我在我的默认包中创建一个SUBMIT文件夹以创建.csv文件时遇到问题,我不确定我是否可以手动为项目执行此操作,或者是否必须通过我的程序创建它们才能工作

import java.io.*;

public class HW01 {
    public static void main(String args[]) throws IOException {

    // Create a 1D array to hold header labels
    String headerLabels[] =  
        {"COURSE ID", "TEAM ID", "STUDENT FIRST NAME",
         "STUDENT LAST NAME", "STUDENT ID", "ASSIGNMENT ID",
         "DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"
        };

    // Create a 2D array to hold header values
    String headerValues[][] =
        {
        {"CMPS280-02", "Invokers01", "James", "Brown", "w0479045", "H01", "8/25/2017", "1:14PM", "James Brown"},
        {"CMPS280-01", "Winners03", "Jacob", "Harley", "w0389342", "H03", "8/23/2017", "7:24PM", "Jacob Harley"},
        {"SE101-02", "CodeIt00", "Keith", "Dillinger", "w0782345", "S04", "8/25/2017", "1:23AM", "Keith Dillinger"}
        };

    // Array Loop to be used later
    //for (int i = 0; i < headerValues.length; i++){
        //for (int j = 0; j < headerValues[i].length; j++){
      }
          }

    // Create new .csv file and store in SUBMIT folder
    String path = "SUBMIT/"+headerValues[0][0]+"_"+headerValues[0][5]+"_"+headerValues[0][1]+"_"+headerValues[0][4]+".csv";
    File file = new File(path);
    FileWriter writer = new FileWriter(file);

    }
 }

2 个答案:

答案 0 :(得分:0)

使用java.nio.file包(这是处理以Java7开头的文件的首选方式):

import java.nio.file.*;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) {
        Path pathToFile = Paths.get("dirname/filename.csv");
        try {
            Files.createDirectories(pathToFile.getParent());
            Files.createFile(pathToFile);
        } catch( IOException e ) {
            System.out.println(e);
        }
    }
}

使用java.io包:

import java.io.*;

public class Demo {
    public static void main(String[] args) {
        File file = new File("dirname", "filename.csv");
        new File(file.getParent()).mkdirs();
        try {
            file.createNewFile();
        } catch( IOException e ) {
            System.out.println(e);
        }
    }
}

答案 1 :(得分:0)

你也可以用旧的方式做。你错过的主要是创建目录,如果它不存在。并且,当你完成它时关闭作者。

package createcsvfile;

import java.io.*;
import java.io.IOException;

public class CreateCsvFile {
    public static void main(String args[]) throws IOException {

        // Create a 1D array to hold header labels
        String headerLabels[] =  
            {"COURSE ID", "TEAM ID", "STUDENT FIRST NAME",
             "STUDENT LAST NAME", "STUDENT ID", "ASSIGNMENT ID",
             "DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"
            };

        // Create a 2D array to hold header values
        String headerValues[][] =
            {
            {"CMPS280-02", "Invokers01", "James", "Brown", "w0479045", "H01", "8/25/2017", "1:14PM", "James Brown"},
            {"CMPS280-01", "Winners03", "Jacob", "Harley", "w0389342", "H03", "8/23/2017", "7:24PM", "Jacob Harley"},
            {"SE101-02", "CodeIt00", "Keith", "Dillinger", "w0782345", "S04", "8/25/2017", "1:23AM", "Keith Dillinger"}
            };

        // Array Loop to be used later
        // for (int i = 0; i < headerValues.length; i++){
        // for (int j = 0; j < headerValues[i].length; j++){

        String path1 = "SUBMIT";    
        // Create new .csv file and store in SUBMIT folder
        String path2 = "SUBMIT/"+headerValues[0][0]+"_"+headerValues[0][5]+"_"+headerValues[0][1]+"_"+headerValues[0][4]+".csv";

        try {
            File file1 = new File(path1);
            if (!file1.isDirectory()) {
               file1.mkdir();
            }        
            File file = new File(path2);        
            FileWriter writer = new FileWriter(file);
            writer.close();
        } 
        catch (IOException e) {
            System.out.println("IOException: " + e );
        }
    }
}