完整内容未写入文件 [英] Full content is not written into a file

查看:43
本文介绍了完整内容未写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件中有以下数据集.

I've the below set of data in my text file.

10
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
542
100
230 863 916 585 981 404 316 785 88 12 70 435 384 778 887 755 740 337 86 92 325 422 815 650 920 125 277 336 221 847 168 23 677 61 400 136 874 363 394 199 863 997 794 587 124 321 212 957 764 173 314 422 927 783 930 282 306 506 44 926 691 568 68 730 933 737 531 180 414 751 28 546 60 371 493 370 527 387 43 541 13 457 328 227 652 365 430 803 59 858 538 427 583 368 375 173 809 896 370 789
789
65
591 955 829 805 312 83 764 841 12 744 104 773 627 306 731 539 349 811 662 341 465 300 491 423 569 405 508 802 500 747 689 506 129 325 918 606 918 370 623 905 321 670 879 607 140 543 997 530 356 446 444 184 787 199 614 685 778 929 819 612 737 344 471 645 726
101
5
722 600 905 54 47
35
51
210 582 622 337 626 580 994 299 386 274 591 921 733 851 770 300 380 225 223 861 851 525 206 714 985 82 641 270 5 777 899 820 995 397 43 973 191 885 156 9 568 256 659 673 85 26 631 293 151 143 423
890
62
286 461 830 216 539 44 989 749 340 51 505 178 50 305 341 292 415 40 239 950 404 965 29 972 536 922 700 501 730 430 630 293 557 542 598 795 28 344 128 461 368 683 903 744 430 648 290 135 437 336 152 698 570 3 827 901 796 682 391 693 161 145
163
90
22 391 140 874 75 339 439 638 158 519 570 484 607 538 459 758 608 784 26 792 389 418 682 206 232 432 537 492 232 219 3 517 460 271 946 418 741 31 874 840 700 58 686 952 293 848 55 82 623 850 619 380 359 479 48 863 813 797 463 683 22 285 522 60 472 948 234 971 517 494 218 857 261 115 238 290 158 326 795 978 364 116 730 581 174 405 575 315 101 99
295
17
678 227 764 37 956 982 118 212 177 597 519 968 866 121 771 343 561

这里第一个数字给出了测试用例的数量,这里是10,后面的数字是总和,下一行是数组的大小,后面是数组元素(数字).在这里,我需要对数组数字求和并与给出的总和相匹配.并打印与总和匹配的数字的位置.

here the first number gives the number of test cases, here it is 10, and the number after it is the sum and the next line is the size of array followed by the array elements(numbers). here i need to sum the array numbers and match with the sum that is given. and print the position of the numbers that match the sum.

在上述情况下,结果应该是.

In the above case the results should be.

2, 3
1, 4
4, 5
29, 46
11, 56
4, 5
40, 46
16, 35
55, 74
7, 9

我一直在尝试使用以下代码将输出写入文件,并且只是为了检查我是否将其打印到控制台.但这里只有最后一个案例结果被更新到文件中.请让我知道我哪里出错了.

I've been trying the below code to write the output into a file, And also just to check i'm printing it even to the console. But here only the last case result is getting updated into file. please let me know where am i going wrong.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class FromFile {

    public static void main(String args[]) throws Exception {
        Scanner s = new Scanner(new File("D:/A1.txt"));
        int testCaseCount = Integer.parseInt(s.next());

        for (int i = 0; i < testCaseCount; i++){
            int Avail = Integer.parseInt(s.next());

            int size=Integer.parseInt(s.next());

            ArrayList<Integer> list = new ArrayList<Integer>();

            for(int j=0;j<size;j++)
            {

            list.add(s.nextInt());
            }

            for(int k=0;k<list.size()-1;k++){
                for(int j=k+1; j<=list.size()-1;j++){
                        int sum=list.get(k)+list.get(j);

                        if(sum==Avail){
                        System.out.println((k+1)+", "+(j+1));
                        File file=new File("D:/A2.txt");
                        if(!file.exists()){
                            file.createNewFile();
                        }
                        FileWriter fw=new FileWriter(file.getAbsoluteFile());
                        BufferedWriter bw=new BufferedWriter(fw);
                        bw.write("Case:"+i+"-"+(k+1)+", "+(j+1)+" Available is "+Avail+" Values are "+list.get(k)+","+ list.get(j));
                        bw.close();
//                      System.out.println("Done");
                        }

                }
                }       


            }
        s.close();

    }
    }

文件中打印的行是

Case:9-7, 9 Available is 295 Values are 118,177

推荐答案

只是最后一个;不为每个元素只打开一次文件.默认情况下,使用 FileWriter 打开文件会清空文件

it is only the last one; open the file only once not for each element.Opening a file with a FileWriter by default empties out the file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class FromFile {

public static void main(String args[]) throws Exception {
    Scanner s = new Scanner(new File("D:/A1.txt"));
    int testCaseCount = Integer.parseInt(s.next());

    for (int i = 0; i < testCaseCount; i++){
        int Avail = Integer.parseInt(s.next());

        int size=Integer.parseInt(s.next());

        ArrayList<Integer> list = new ArrayList<Integer>();

        for(int j=0;j<size;j++)
        {

        list.add(s.nextInt());
        }

        FileWriter fw=new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw=new BufferedWriter(fw);

        for(int k=0;k<list.size()-1;k++){
            for(int j=k+1; j<=list.size()-1;j++){
                    int sum=list.get(k)+list.get(j);

                    if(sum==Avail){
                    System.out.println((k+1)+", "+(j+1));
                    File file=new File("D:/A2.txt");
                    if(!file.exists()){
                        file.createNewFile();
                    }

                    bw.write("Case:"+i+"-"+(k+1)+", "+(j+1)+" Available is "+Avail+" Values are "+list.get(k)+","+ list.get(j));


                    }

            }
            }       

         bw.close();
        }
    s.close();

}
}

这篇关于完整内容未写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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