2016-01-21 90 views
-2

我正在处理打开文本文件并将其转换/保存为sql文件的任务。我得到了如何打开一个文本文件,但我坚持如何转换为SQL文件。将一个文本文件转换为一个sql文件

下面是代码,我得到了阅读的文本文件

public static void main(String[] args) { 

     // The path to open the file. 
     String fileName = "input.txt"; 
     String line = null; 

     try { 
      // The file reads text files 
      FileReader file = new FileReader(fileName); 

      // Wrap file in BufferedReader 
      BufferedReader bufferedReader = new BufferedReader(file); 

      while ((line = bufferedReader.readLine()) != null) { 
       System.out.println(line); 
      } 

      // Close files 
      bufferedReader.close(); 
     } catch (FileNotFoundException ex) { 
      System.out.println("Unable to open file '" + fileName + "'"); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

你能有人给我一些提示如何保存文本文件作为SQL文件中读取文本文件后?

非常感谢!

+1

只需在每行中用','替换'|'。 – Satya

+0

@Satya谢谢,但读取文本文件没有错,输出仍显示'|'。我被困在如何将文本文件转换为sql文件,如上面给出的示例输出 –

+0

'|'是正则表达式中的一个特殊字符。像'// |'一样使用。 – Satya

回答

1

修改您的尝试块,如下所示。 添加了两个功能List<String> parseLine(String line)String createInsert(List<String> headers, List<String> rowData)

实现第一个使用简单的字符串标记化,然后通过简单的字符串连接使用createInsert。

try { 
     // The file reads text files 
     FileReader file = new FileReader(fileName); 

     // Wrap file in BufferedReader 
     BufferedReader bufferedReader = new BufferedReader(file); 
     List<String> headers; 
     String line = bufferedReader.readLine(); 
     if(line !=null) { //got the row header 
      headers = parseLine(line); 
     } 
     List<String> rowData; 
     while ((line = bufferedReader.readLine()) != null) { 
      rowData = parseLine(line); 
      createInsert(headers, rowData); 
     } 
     // Close files 
     bufferedReader.close(); 
    } catch (FileNotFoundException ex) { 
     System.out.println("Unable to open file '" + fileName + "'"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
+0

非常感谢!你能解释更多关于字符串标记和createInsert吗?我不熟悉这两个术语。 –

0

查找下面的几个步骤

public static void main(String[] args) throws Exception { 
    // read all lines into a list of strings 
    List<String> lines = Files.readAllLines(Paths.get("data.txt"), 
      StandardCharsets.UTF_8); 
    // use the first string to generate the INSERT statement 
    // without the values 
    String insertLine = createInsertLine(lines.get(0)); 
    try (BufferedWriter bw = Files.newBufferedWriter(Paths.get("data.sql"), 
      StandardCharsets.UTF_8)) { 
     bw.write(insertLine); 
     bw.write(System.lineSeparator()); 
     int lastValueId = lines.size() - 1; 
     for (int i = 1; i <= lastValueId; i++) { 
      // create from each following line the VALUES part 
      // of the INSERT statement 
      String valueLine = createValueLine(i, lines.get(i)); 
      // write the lines into the file 
      bw.write(valueLine); 
      bw.write(i == lastValueId ? ';' : ','); 
      bw.write(System.lineSeparator()); 
     } 
    } 
} 

private static String createValueLine(int id, String line) { 
    // split the values in the line on the '|' character 
    // including leading and trailing blanks 
    String[] columns = line.split(" *\\| *"); 
    // construct the VALUES line 
    StringBuilder valueLine = new StringBuilder(); 
    // start with the ID value 
    valueLine.append("(").append(id).append(", "); 
    // append all column values, except for the last column 
    for (int i = 0; i < columns.length - 1; i++) { 
     valueLine.append('\'') 
       .append(columns[i]) 
       .append("', "); 
    } 
    // append the last column value and the end of the line 
    valueLine.append('\'') 
      .append(columns[columns.length - 1]) 
      .append("')"); 
    return valueLine.toString(); 
} 

private static String createInsertLine(String line) { 
    String[] columns = line.split(" *\\| *"); 
    StringBuilder insertLine = new StringBuilder(); 
    insertLine.append("INSERT INTO 'events' ('id', "); 
    for (int i = 0; i < columns.length - 1; i++) { 
     insertLine.append('\'') 
       .append(columns[i]) 
       .append("', "); 
    } 
    insertLine.append('\'') 
      .append(columns[columns.length - 1]) 
      .append("') VALUES"); 
    return insertLine.toString(); 
} 

假设该文件data.txt解释工作段包含

Selected | Status | Event | File 
Yes | Listed | Birthday | bday.pdf 
No | Not Listed | Gifts | gifts.pdf 

生成的data.sql

INSERT INTO 'events' ('id', 'Selected', 'Status', 'Event', 'File') VALUES 
(1, 'Yes', 'Listed', 'Birthday', 'bday.pdf'), 
(2, 'No', 'Not Listed', 'Gifts', 'gifts.pdf'); 
+0

非常感谢,它对我来说真的很好。但我正在尝试像'(1,'是','上市','生日','bday.pdf'),而不是'(1,'是','列出','生日',' bday.pdf');'还有'INSERT INTO'事件'('id','Selected','Status','Event','File')VALUES'只能显示一次头。我该如何解决这些问题?再次感谢! –

+0

@MichaelNg ** 1)**在'createValueLine'中改变'.append('');“);'在'.append(”'),“); ** 2)**将行' bw.write(insertLine);'在'for int i = 1 ...'之前的'main'方法中。 – SubOptimal

+0

你没有回答我的第二部分,非常感谢!但对于第一个问题,我试图做的只是最后一行有';'最后,它应该看起来像INSERT INTO'events'('id','Selected','Status','Event' ,'文件')VALUES(1,'是','列出','生日','bday.pdf'), (2,'不','未列出','Gifts','gifts.pdf ');' –

相关问题