【问题标题】:Java one file to anothe file appendingJava将一个文件附加到另一个文件
【发布时间】:2018-12-06 19:17:21
【问题描述】:

我有两个数据文件Test1和Test2。

Test1 数据:

1.0  2.0  3.0
2.0  4.0  5.0
3.0  1.0  2.0
4.0  9.0  2.0
7.0  0.0  0.0
8.0  7.0  1.0
10.0 2.0  5.0
11.0 0.0  0.0
13.0 1.0  1.0
15.0 1.0  1.0
17.0 2.0  2.0

Test2.txt

 5.0   2.0   5.0   6.0
 6.0   1.0   2.0   9.0
 9.0   0.0   1.0   3.0
 12.0  0.0   0.0   0.0
 14.0  1.0   1.0   1.0

test4.txt(输出文件)

 1.0  2.0  3.0
 2.0  4.0  5.0
 3.0  1.0  2.0
 4.0  9.0  2.0
 5.0   2.0   5.0   6.0
 6.0   1.0   2.0   9.0
 7.0  0.0  0.0
 8.0  7.0  1.0
 9.0   0.0   1.0   3.0
 10.0 2.0  5.0
 11.0 0.0  0.0
 12.0  0.0   0.0   0.0
 13.0 1.0  1.0
 14.0  1.0   1.0   1.0
 15.0 1.0  1.0
 17.0 2.0  2.0

我的代码:

public class Insertelementfile {
public static void main(String args[])throws IOException{
    Scanner X=new Scanner(new File("c:\\test1.txt"));
    Scanner Y=new Scanner(new File("C:\\test2.txt"));
    PrintWriter write1=null;
     write1= new PrintWriter(new File("C:\\test4.txt"));
    double a=0.0,d=0.0;
    int i=0,j=0,k=0;
    List<Double> list1=new ArrayList<>();
    List<Double>list2=new ArrayList<>();
    double[]arrX=new double[11];
    double[]arrY=new double[5];
    double[] s = new double[16];
    while(X.hasNext()){
        a = X.nextDouble();
        list1.add(a);
        double b=X.nextDouble();
        double c=X.nextDouble();
    }
    while(Y.hasNext()) {
        d = Y.nextDouble();
        list2.add(d);
        double e=Y.nextDouble();
        double f=Y.nextDouble();
        double g=Y.nextDouble();
    }
    for(int i1=0;i1<list1.size();i1++) {

        arrX[i1]=list1.get(i1);
    }

    for(int j1=0;j1<list2.size();j1++){
        arrY[j1]=list2.get(j1);
    }
    while(i<arrX.length && j<arrY.length){
        if (arrX[i] < arrY[j]){
            s[k]=arrX[i];
            i++;
        }
        else{
            s[k] = arrY[j];
            j++;
        }
        k++;
    }

    // Copy the remaining elements in arrX to s
    if (i < arrX.length) {
        // arraycopy(source, sourcepos, dest, destpos, numOfElements)
        System.arraycopy(arrX, i, s, k, (arrX.length - i));
    }


    // Copy the remaining elements in arrY to s
    if (j < arrY.length) {
        // arraycopy(source, sourcepos, dest, destpos, numOfElements)
        System.arraycopy(arrY, j, s, k, (arrY.length - j));
    }
    System.out.println( Arrays.toString(s));
    write1.write(Arrays.toString(s));
    write1.flush();
    write1.close();
}
}

我得到的输出是这样的: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 17.0] 但是我的输出中没有剩余的列。我将两个文件的第一列作为我的关键属性并根据那里的顺序插入元素。我应该怎么做才能得到我想要的输出?

【问题讨论】:

  • 在您的第一个 for 循环中,您正在读取一行的第二个和第三个值,但您没有将它们存储在任何地方,它们根本没有被使用。此外,我强烈建议您更好地命名变量。例如,使用“test1SecondValue”代替“b”可以更容易理解发生了什么。
  • 另外,您可以简单地调用 arraylist.toArray(),而不是手动将 ArrayList 转换为带有循环的数组。
  • 我怎样才能得到我想要的输出?我得到的输出是整个输出的一部分。

标签: java arrays list append


【解决方案1】:

我会创建一个类来保存您的数据。例如:

public class MyData implements Comparable<MyData>{
    public ArrayList<Double> data;

    public MyData(){
        data = new ArrayList<Double>();
    }

    @Override
    public int compareTo(MyData otherData) {
        if(otherData == null){
            return 1;
        }
        if(data.size() != 0 && otherData.data.size() != 0){
            if(otherData.data.get(0) >= data.get(0)){
                return -1;
            } else {
                return 1;
            }
        }
        return 0;
    }

}

然后您可以保留一个 ArrayList,将一行值读入一个 MyData 对象。然后,您可以使用 Collections.sort(that arraylist) 对其进行排序,然后对其进行迭代并将其打印到您的文件中。

【讨论】:

  • 使用那个 MyData 类来存储你的行。当您使用扫描仪循环时,将 MyData 添加到数组列表中,然后将您从扫描仪获得的双精度数据添加到 MyData 中的数组列表中。这样,您就可以将线路的所有信息保存在一起。此外,您可以使用 Collections.sort 轻松对其进行排序,这意味着您可以对其进行排序,然后循环遍历列表并将其写入文件。您无法理解哪些具体部分?我很乐意提供帮助,但如果我不知道您遇到了什么问题,我将无能为力。
  • 我不需要排序。数据已经排序。但是数据之间存在一些差距,比如在 test1 的 4 之后缺少 5。我们只需要填补这些空白。你写的代码不清楚。什么是数据?什么是其他数据?如何将此代码与我的代码链接?能否请您修改我的代码,以便我理解。
猜你喜欢
  • 2019-08-15
  • 2018-02-07
  • 2020-03-19
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
相关资源
最近更新 更多