【问题标题】:Swap two columns of a file in java [closed]在java中交换文件的两列[关闭]
【发布时间】:2015-10-19 21:55:33
【问题描述】:

我正在尝试编写一种使用 java 交换文本文件列的方法。

有人可以告诉我如何编写一个方法来交换 java 中文件的两列吗?两列用空格隔开

【问题讨论】:

  • 我投票结束这个问题,因为它是一个“给我代码”的问题,不适合 Stack Overflow。
  • 我只是问有什么办法......“我没有要求代码”......我想要像“nbryans”给我的答案......

标签: java indexoutofboundsexception


【解决方案1】:

一种可能的方法:

【讨论】:

    【解决方案2】:

    读取文件的每一行

    ArrayList<String[]> aryL = new ArrayList<String[]>();
    
    for each line of file
    
        aryl.add(eachline.split(","));
    
    for(String[] sArr: arrL)
    
        //Swap the elements and print or write to file
    

    【讨论】:

      【解决方案3】:

      你可以读取文件的每一行并尝试

      String buffer = "";
      //for each line of input 
      String[] columns = line.split(" ");
      buffer+= columns[1] + " " + columns[0] + "\n";
      //end for
      

      然后用你的缓冲区字符串覆盖文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多