尝试一次写多个文件 - Java [英] Trying to Write Multiple Files at Once - Java

查看:377
本文介绍了尝试一次写多个文件 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字典文件分割成多个不同长度的字典,例如如果我想把它放在更小的字典中,长度为2,3,4,... n,其中我可以更快地搜索它们。当我说得更快时,我的意思是我会知道输入长度,因此访问相应的长度字典(整体的一小部分)意味着更快的访问。这是我目前的生成文件的实现,但不像我想要的那样写入它们。理想情况下,例如长度为2的所有单词将被写入长度为2的文本文件。有人有什么建议吗?

  import java.io.BufferedReader; 
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException;

public class Main {

public static void main(String [] args)throws IOException {
FileReader fr = new FileReader(dictionary.txt);
PrintWriter l2 = new PrintWriter(dictionary_length2.txt,UTF-8);
PrintWriter l3 = new PrintWriter(dictionary_length3.txt,UTF-8);
PrintWriter l4 = new PrintWriter(dictionary_length4.txt,UTF-8);
PrintWriter l5 = new PrintWriter(dictionary_length5.txt,UTF-8);
PrintWriter l6 =新的PrintWriter(dictionary_length6.txt,UTF-8);
PrintWriter l7 = new PrintWriter(dictionary_length7.txt,UTF-8);
PrintWriter l8 = new PrintWriter(dictionary_length8.txt,UTF-8);
PrintWriter l9 = new PrintWriter(dictionary_length9.txt,UTF-8);
PrintWriter l10 = new PrintWriter(dictionary_length10.txt,UTF-8);
PrintWriter l11 = new PrintWriter(dictionary_lengty11.txt,UTF-8);
PrintWriter l12 = new PrintWriter(dictionary_length12.txt,UTF-8);
PrintWriter l13 = new PrintWriter(dictionary_length13.txt,UTF-8);
PrintWriter l14 = new PrintWriter(dictionary_length14.txt,UTF-8);
PrintWriter l15 = new PrintWriter(dictionary_length15.txt,UTF-8);
PrintWriter l16 = new PrintWriter(dictionary_length16.txt,UTF-8);
PrintWriter l17 = new PrintWriter(dictionary_length17.txt,UTF-8);
PrintWriter l18 = new PrintWriter(dictionary_length18.txt,UTF-8);
PrintWriter l19 = new PrintWriter(dictionary_length19.txt,UTF-8);
PrintWriter l20 = new PrintWriter(dictionary_length20.txt,UTF-8);
PrintWriter l21 = new PrintWriter(dictionary_length21.txt,UTF-8);

BufferedReader tr = new BufferedReader(fr);
String temp;
int temp_length; (int i = 0; i <60388; i ++){
temp = new String(tr.readLine());


temp_length = temp.length();
if(temp_length == 2)
l2.println(temp);
if(temp_length == 3)
l3.println(temp);
if(temp_length == 4)
l4.println(temp);
if(temp_length == 5)
l5.println(temp);
if(temp_length == 6)
l6.println(temp);
if(temp_length == 7)
l7.println(temp);
if(temp_length == 8)
l8.println(temp);
if(temp_length == 9)
l9.println(temp);
if(temp_length == 10)
l10.println(temp);
if(temp_length == 11)
l11.println(temp);
if(temp_length == 12)
l12.println(temp);
if(temp_length == 13)
l13.println(temp);
if(temp_length == 14)
l14.println(temp);
if(temp_length == 15)
l15.println(temp);
if(temp_length == 16)
l16.println(temp);
if(temp_length == 17)
l17.println(temp);
if(temp_length == 18)
l18.println(temp);
if(temp_length == 19)
l19.println(temp);
if(temp_length == 20)
l20.println(temp);
if(temp_length == 21)
l21.println(temp);
}

tr.close();
l2.close();
l3.close();
l4.close();
l5.close();
l6.close();
l7.close();
l8.close();
l9.close();
l10.close();
l11.close();
l12.close();
l13.close();
l14.close();
l15.close();
l16.close();
l17.close();
l18.close();
l19.close();
l20.close();
l21.close();
System.out.println(Complete);
}
}


解决方案

答案如下。 (这个应该也打印文件的内容,除非我缺少一些非常基本的东西。)






每当有一组变量形式为 xN (例如 l2 l3 l22 ),他们应该通常使用List集合类型替换 ,例如 ArrayList



这只是一个例子,显示如何减少重复和固定边界

  int MAX_WORD_LEN = 22; //使这个动态左边作为一个excercise 
列表< PrintWriter> writers = new ArrayList< PrintWriter>(); (int i = 0; i <= MAX_WORD_LEN; i ++){
PrintWriter w = new PrintWriter(dictionary_length+ i +.txt,UTF-8 );
writers.Add(w);
}

字符串;
while((line = tr.readLine())!= null){
int len = line.length();
if(len< writers.size()){
writers.get(len).println(line);
}
}

(PrintWriter w:writers){
w.close();
}

可以轻微调整以创建0或1档案。


I am trying to split a dictionary file that I have into multiple dictionaries of different lengths, for example if I want take it and put it into smaller dictionaries of length 2, 3, 4, ..... n, where I can then search them quicker. When I say quicker I mean that I will know the input length and therefore accessing the corresponding length dictionary (a fraction of the whole) will mean quicker accesses. This is my current implementation that generates the files but doesn't write to them like I desire. Ideally, all words of length 2 for example will be written into the length2 text file. Anyone have any suggestions?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("dictionary.txt");
    PrintWriter l2 = new PrintWriter("dictionary_length2.txt", "UTF-8");
    PrintWriter l3 = new PrintWriter("dictionary_length3.txt", "UTF-8");
    PrintWriter l4 = new PrintWriter("dictionary_length4.txt", "UTF-8");
    PrintWriter l5 = new PrintWriter("dictionary_length5.txt", "UTF-8");
    PrintWriter l6 = new PrintWriter("dictionary_length6.txt", "UTF-8");
    PrintWriter l7 = new PrintWriter("dictionary_length7.txt", "UTF-8");
    PrintWriter l8 = new PrintWriter("dictionary_length8.txt", "UTF-8");
    PrintWriter l9 = new PrintWriter("dictionary_length9.txt", "UTF-8");
    PrintWriter l10 = new PrintWriter("dictionary_length10.txt", "UTF-8");
    PrintWriter l11 = new PrintWriter("dictionary_lengty11.txt", "UTF-8");
    PrintWriter l12 = new PrintWriter("dictionary_length12.txt", "UTF-8");
    PrintWriter l13 = new PrintWriter("dictionary_length13.txt", "UTF-8");
    PrintWriter l14 = new PrintWriter("dictionary_length14.txt", "UTF-8");
    PrintWriter l15 = new PrintWriter("dictionary_length15.txt", "UTF-8");
    PrintWriter l16 = new PrintWriter("dictionary_length16.txt", "UTF-8");
    PrintWriter l17 = new PrintWriter("dictionary_length17.txt", "UTF-8");
    PrintWriter l18 = new PrintWriter("dictionary_length18.txt", "UTF-8");
    PrintWriter l19 = new PrintWriter("dictionary_length19.txt", "UTF-8");
    PrintWriter l20 = new PrintWriter("dictionary_length20.txt", "UTF-8");
    PrintWriter l21 = new PrintWriter("dictionary_length21.txt", "UTF-8");

    BufferedReader tr = new BufferedReader(fr);
    String temp;
    int temp_length;

    for(int i = 0; i < 60388; i++){
        temp = new String(tr.readLine());
        temp_length = temp.length();
        if(temp_length == 2)
            l2.println(temp);
        if(temp_length == 3)
            l3.println(temp);
        if(temp_length == 4)
            l4.println(temp);
        if(temp_length == 5)
            l5.println(temp);
        if(temp_length == 6)
            l6.println(temp);
        if(temp_length == 7)
            l7.println(temp);
        if(temp_length == 8)
            l8.println(temp);
        if(temp_length == 9)
            l9.println(temp);
        if(temp_length == 10)
            l10.println(temp);
        if(temp_length == 11)
            l11.println(temp);
        if(temp_length == 12)
            l12.println(temp);
        if(temp_length == 13)
            l13.println(temp);
        if(temp_length == 14)
            l14.println(temp);
        if(temp_length == 15)
            l15.println(temp);
        if(temp_length == 16)
            l16.println(temp);
        if(temp_length == 17)
            l17.println(temp);
        if(temp_length == 18)
            l18.println(temp);
        if(temp_length == 19)
            l19.println(temp);
        if(temp_length == 20)
            l20.println(temp);
        if(temp_length == 21)
            l21.println(temp);
    }

    tr.close();
    l2.close();
    l3.close();
    l4.close();
    l5.close();
    l6.close();
    l7.close();
    l8.close();
    l9.close();
    l10.close();
    l11.close();
    l12.close();
    l13.close();
    l14.close();
    l15.close();
    l16.close();
    l17.close();
    l18.close();
    l19.close();
    l20.close();
    l21.close();
    System.out.println("Complete.");
}
}

解决方案

Tangental "answer" follows. (This should also print out contents to the files, unless I'm missing something very basic.)


Whenever there is a set of variables in the form xN (e.g. l2, l3, l22), they should usually be replaced with a List collection type such as an ArrayList.

This is just an example to show how can be reduce duplication and fixed bounds:

int MAX_WORD_LEN = 22; // making this dynamic left as an excercise
List<PrintWriter> writers = new ArrayList<PrintWriter>();

for (int i = 0; i <= MAX_WORD_LEN; i++) {
    PrintWriter w = new PrintWriter("dictionary_length" + i + ".txt", "UTF-8");
    writers.Add(w);
}

String line;
while ((line = tr.readLine()) != null) {
   int len = line.length();
   if (len < writers.size()) {
       writers.get(len).println(line);
   }
}

for (PrintWriter w : writers) {
    w.close();
}

Slight adjustments can be made to not create a "0" or "1" file.

这篇关于尝试一次写多个文件 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆