JAVA:一起读写文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4251058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:32:05  来源:igfitidea点击:

JAVA : read and write a file together

javafilefile-iojava-io

提问by sharath

I am trying to read a java file and modify it simultaneously. This is what I need to do : My file is of the format :

我正在尝试读取一个 java 文件并同时修改它。这就是我需要做的:我的文件格式为:

aaa
bbb
aaa
ccc
ddd
ddd

I need to read through the file and get the count of the # of occurrences and modify the duplicates to get the following file:

我需要通读文件并获取出现次数的计数并修改重复项以获取以下文件:

aaa -  2
bbb -  1
ccc -  1
ddd -  2

I tried using the RandomAccessFileto do this, but couldn't do it. Can somebody help me out with the code for this one?

我尝试使用RandomAccessFile来做到这一点,但无法做到。有人可以帮我解决这个问题的代码吗?

回答by J _

It's far easier if you don't do two things at the same time. The best way is to run through the entire file, count all the occurrences of each string in a hash and then write out all the results into another file. Then if you need to, move the new file over the old one.

如果你不同时做两件事,事情会容易得多。最好的方法是遍历整个文件,计算每个字符串在哈希中的所有出现次数,然后将所有结果写到另一个文件中。然后,如果需要,将新文件移到旧文件上。

You never want to read and write to the same file at the same time. Your offsets within the file will shift everytime you make a write and the read cursor will not keep track of that.

您永远不想同时读取和写入同一个文件。每次写入时,文件中的偏移量都会发生变化,而读取光标不会跟踪该偏移量。

回答by Benjamin Stadin

I'd do it this way: - Parse the original file and save all entries into a new file. Use fixed length data blocks to write entries to the new file (so, say your longest string is 10 bytes long, take 10 + x as block length, x is for the extra info you want to save along the entries. So the 10th entry in the file would be at byte position 10*(10+x)). You'd also have to know the number of entries to create the (so the file size would noOfEntries*blocklength, use a RandomAccesFile and setLength to set the this file length). - Now use quicksort algorithm to sort the entries in the file (my idea is to have a sorted file in the end which makes things far easier and faster finally. Hashing would theoretically work too, but you'd have to deal with rearranging duplicate entries then to have all duplicates grouped - not really a choice here). - Parse the file with the now sorted entries. Save a pointer to the entry of the first occurence of a entry. Increment the number of duplicates until there is a new entry. Change the first entry and add that additonal info you want to have there into a new "final result" file. Continue this way with all remaining entries in the sorted file.

我会这样做: - 解析原始文件并将所有条目保存到一个新文件中。使用固定长度的数据块将条目写入新文件(因此,假设您最长的字符串是 10 个字节长,取 10 + x 作为块长度,x 用于您想要沿条目保存的额外信息。所以第 10 个条目在文件中将在字节位置 10*(10+x))。您还必须知道要创建的条目数(因此文件大小为 noOfEntries*blocklength,使用 RandomAccesFile 和 setLength 来设置此文件长度)。- 现在使用快速排序算法对文件中的条目进行排序(我的想法是最后有一个排序的文件,这最终使事情变得更容易和更快。散列理论上也可以工作,但你必须处理重新排列重复的条目然后将所有重复项分组 - 这里不是真正的选择)。- 使用现在排序的条目解析文件。保存指向条目第一次出现的条目的指针。增加重复的数量,直到有新条目。更改第一个条目并将您想要的附加信息添加到新的“最终结果”文件中。继续以这种方式处理排序文件中的所有剩余条目。

Conclusions: I think this should be a reasonably fast and use reasonable amount of resources. However, it depends on the data you have. If you have a very large number of duplicates, quicksort performance will degrade. Also, if your longest data entry is way longer than the average, it will also waste file space.

结论:我认为这应该是一个相当快的速度并使用合理数量的资源。但是,这取决于您拥有的数据。如果您有大量重复项,则快速排序性能会下降。此外,如果最长的数据条目比平均值长得多,也会浪费文件空间。

回答by greppz

If you have to, there are ways you can manipulate the same file and update the counters, without having to open another file or keep everything in memory. However, the simplest of the approaches would be very slow.

如果必须,您可以通过多种方法操作同一个文件并更新计数器,而无需打开另一个文件或将所有内容保存在内存中。然而,最简单的方法会非常缓慢。

回答by Neha Sharma

 import java.util.*;
 import java.io.*;
 import java.util.*;
 class WordFrequencyCountTest
 {
 public static void main( String args[])
 {
System.out.println(" enter the file name");
Scanner sc = new Scanner(System.in);
String fname= sc.next();    
     File f1 = new File(fname);


    if(!f1.exists())
    {
        System.out.println(" Source file doesnot exists");
        System.exit(0);
    }
    else{
        try{                
            FileReader fis = new FileReader(f1);
            BufferedReader br = new BufferedReader(fis);
            String str = "";
            int count=0;  
        Map<String, Integer> map = new TreeMap<String, Integer>(); 
            while((str = br.readLine()) != null )
            {
                String[] strArray = str.split("\s");
                count=1;
                for(String token : strArray)   // iteration of strArray []
                {                       
                if(map.get(token)!=null )
            {
                        count=map.get(token);
                        count++;
                        map.put(token, count);
                        count=1;
                    }else{
                        map.put(token, count);

                    }
                }
            }

            Set set=map.entrySet();
            Iterator itr = set.iterator();    
            System.out.println("========");

            while(itr.hasNext())
            {
                Map.Entry entry = (Map.Entry)itr.next();

                System.out.println( entry.getKey()+ " "+entry.getValue());
            }               
            fis.close();            
        }catch(Exception e){}
           }
        }
    }