2017-05-13 74 views
0

我有大约1 GB的大数据集,需要将其从.txt转换为.arff格式。所有来源从.txt转换为.csv,然后转换为.arff,但使用在线转换器时,最大大小为100 MB。任何人都可以帮助我如何转换这1 GB的数据?将.txt中的大数据集转换为.arff格式

回答

0

您可以使用此代码段将txt文件转换为csv。

import java.io.*; 

public class Convert { 
public static void main(String[] args) { 
    System.out.println("Initialize ..."); 
    Read read = new Read("soal01.txt"); 
    Print print = new Print(); 
    for (int i = 1; i <= 3; i++) { 
     try{ 
      print.setFileName("jawaban"+i+".csv"); 
      read.setPrint(print); 
      read.exec(i); 
     } catch(IOException ex){ 
      ex.printStackTrace(); 
     } 
    } 
    System.out.println("Finished"); 
    } 
} 

class Print { 

protected String fileName; 

protected FileWriter writer; 

public Print() {} 

public Print(String fileName) throws IOException { 
    this.fileName = fileName; 
    this.writer = new FileWriter(this.fileName); 
} 

public String getFileName() { 
    return this.fileName; 
} 

public void setFileName(String fileName) throws IOException { 
    this.fileName = fileName; 
    this.writer = new FileWriter(this.fileName); 
} 

public void close() throws IOException { 
    this.writer.flush(); 
    this.writer.close(); 
} 

public void addRow(String[] c) throws IOException { 
    int l = c.length; 
    for(int i = 0; i < l; i++) { 
     this.writer.append(c[i]); 
     if(i != (l - 1)) { 
      this.writer.append(","); 
     } 
    } 
    this.writer.append('\n'); 
    } 
} 

class Read { 

protected String fileName; 

protected BufferedReader bufferedReader; 

protected Print print; 

public Read(String fileName) { 
    this.fileName = fileName; 
} 

public Read(Print print, String fileName) { 
    this.print = print; 
    this.fileName = fileName; 
} 

public void setPrint(Print print) { 
    this.print = print; 
} 

public Print getPrint() { 
    return this.print; 
} 

public void exec(Integer type) { 
    String sCurrentLine = ""; 
    try{ 
     this.bufferedReader = new BufferedReader(
      new FileReader(this.fileName)); 

     while((sCurrentLine = this.bufferedReader.readLine()) != null) { 
      String[] columns = sCurrentLine.split("\t"); 
      int length = columns.length; 
      if(type == 1) { 
       this.print.addRow(columns); 
      } else if(length >= 2){ 
       double col1 = Double.parseDouble(columns[1]); 
       if(col1 > 20){ 
        if(type == 2){ 
         this.print.addRow(columns); 
        } else if(type == 3 && length >= 3 && columns[2] != null){ 
         this.print.addRow(columns); 
        } 
       } 
      } 
     } 
     this.print.close(); 

    } catch(IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     try{ 
      if(this.bufferedReader != null) { 
       this.bufferedReader.close(); 
      } 
     } catch(IOException ex) { 
      ex.printStackTrace(); 
     } 
     } 
    } 
} 
+0

谢谢你的帮助,但我运行的代码,并出现以下错误。 异常线程“main” java.lang.NumberFormatException:对于输入字符串:“青少年分享如何基督教影响到他们生活” \t在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) \t在阳光下.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) \t在java.lang.Double.parseDouble(Double.java:538) \t在Read.exec(Convert.java:97) \t在Convert.main( Convert.java:12) – medooSa

相关问题