2017-08-26 95 views
-1

我在我的默认包中创建SUBMIT文件夹以创建.csv文件时遇到问题,并且我不确定是否可以为项目手动执行该操作,还是必须通过我的程序创建才能工作。如何创建一个文件夹来进一步创建.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); 

    } 
} 

回答

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); 
     } 
    } 
} 
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); 
     } 
    } 
}