与LinkedList读取和写入.txt文件相关的问题(文件内容读取与写入不同)

时间:2018-05-01 08:33:30

标签: java string file-handling singly-linked-list

我正在读一个链接列表中的.txt文件,然后我将这个链表分成两个大小相等的链接列表。 然后我将这两个(组件链接列表)写在两个单独的文件中。保存这些文件后,我需要读取这些文件并获取它们的内容以合并它们以形成原始数据。

但是当我读取组件文件时,我发现链接列表的大小与写入的大小有所不同。

例如,如果我的原始data_LinkedList的大小为402,那么我的两个组件linkedList每个大小为201,在写入文件然后再次读取之后,每个组件的大小显示为208或209。结果,这些组件不会合并回来形成原始数据。

  public class MyFramework {


public static void write(List<String> doublencC, List<String> doubleencR)
{
     String ec="...C Component File Path";

      try {
        FileWriter fileWriter =  new FileWriter(ec);
        BufferedWriter bufferedWriter =new BufferedWriter(fileWriter);

       for(String c:doublencC)
       {   bufferedWriter.write(c);
           bufferedWriter.newLine();

       }
        bufferedWriter.flush();
        bufferedWriter.close();


     String er="...R Component File Path";
     FileWriter  fileWriter2 =  new FileWriter(er);    
     BufferedWriter  bufferedWriter2 =new BufferedWriter(fileWriter2);
       for(String re : doubleencR){
           bufferedWriter2.write(re);
           bufferedWriter2.newLine();
       }
      bufferedWriter2.flush();
      bufferedWriter2.close();
    }
    catch(IOException ex) {
        System.out.println("Error writing to file");

    }

}

public static List <String> [] readFun() throws IOException
{
    String ec="...C Component File Path";
    File D = new File(ec);
   List<String> si = FileUtils.readLines(D);




    String er="...R Component File Path";
    File D1 = new File(er);
   List<String> s2 = FileUtils.readLines(D1);

   List <String> [] myread = new LinkedList [2];
      myread[0]= new LinkedList<String>(si);
      myread[1]= new LinkedList<String>(s2);




  return myread;
}


public static void main(String[] args) throws IOException {

    File D = new File("...myFile path");

    List<String> data = FileUtils.readLines(D);

List<String> C = new LinkedList <String>(data.subList(0, (data.size()+1)/2));
List<String> R = new LinkedList <String>(data.subList((data.size()+1)/2,data.size()) );
System.out.println(C.size()+" "+R.size());





 System.out.println();
 System.out.println();


 write(C,R);

 List <String> [] myread = new LinkedList [2];
 myread = readFun();

 doubleencC= new LinkedList<String>();
 doubleencR= new LinkedList<String>();

 doubleencC= myread[0];
 doubleencR= myread[1];





  JOptionPane.showMessageDialog(null,"ReadFile Sizes "+doubleencC.size()+" 
  "+doubleencR.size()+" WrittenFile Sizes "+C.size()+" "+R.size());





     }

   }

0 个答案:

没有答案