完整内容不被写入到文件中 [英] Full content is not written into a file

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

问题描述

我有下面的一组数据在我的文本文件。

  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

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 7​​95 28 344 128 461 368 683 903 744 430 648 290 135 437 336 152 698 570 827 3 796 901 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 863 48 813 797 463 683 285 22 60 522 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,和之后数之和下一行是阵列的随后数组元素(数)的大小。在这里,我需要总结的数组号,匹配与获得的款项。并打印匹配的总和的数字的位置。

在上面的情况下的结果应

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

我一直在尝试下面的code写的输出到一个文件中,也只是为了检查我是我,甚至打印到控制台。但这里只有最后一种情况的结果得到更新到文件中。请让我知道我要去哪里错了。

 进口java.io.BufferedWriter中;
进口的java.io.File;
进口java.io.FileWriter;
进口的java.io.PrintWriter;
进口的java.util.ArrayList;
进口java.util.Scanner中;公共类FROMFILE {    公共静态无效的主要(字符串ARGS [])抛出异常{
        扫描器S =新的扫描仪(新File(D:/A1.txt));
        INT testCaseCount =的Integer.parseInt(s.next());        的for(int i = 0; I< testCaseCount;我++){
            INT效用=的Integer.parseInt(s.next());            INT大小=的Integer.parseInt(s.next());            ArrayList的<整数GT;名单=新的ArrayList<整数GT;();            对于(INT J = 0; J<大小; J ++)
            {            list.add(s.nextInt());
            }            对于(INT K = 0; K<则为list.size() - 1; k ++){
                对于(INT J = K + 1; J< =则为list.size() - 1; J ++){
                        INT总和= list.get(K)+ list.get(J);                        如果(总和==效用){
                        的System.out.println((K + 1)+,+第(j + 1));
                        档案文件=新的文件(D:/A2.txt);
                        如果(!file.exists()){
                            file.createNewFile();
                        }
                        FileWriter的FW =新的FileWriter(file.getAbsoluteFile());
                        BufferedWriter将体重=新的BufferedWriter(FW);
                        bw.write(案例:+ I + - +(K + 1)+,+(J + 1)+可为+ +效用价值观是+ list.get(K)+, + list.get(J));
                        bw.close();
//的System.out.println(完成);
                        }                }
                }
            }
        S.CLOSE();    }
    }

文件中的印刷线路

 案例:9-7,9可用295的值是118177


解决方案

这仅仅是最后一个;打开文件只有一次而不是针对每个默认element.Opening一个文件,一个FileWriter清空出文件

 进口java.io.BufferedWriter中;
进口的java.io.File;
进口java.io.FileWriter;
进口的java.io.PrintWriter;
进口的java.util.ArrayList;
进口java.util.Scanner中;公共类FROMFILE {公共静态无效的主要(字符串ARGS [])抛出异常{
    扫描器S =新的扫描仪(新File(D:/A1.txt));
    INT testCaseCount =的Integer.parseInt(s.next());    的for(int i = 0; I< testCaseCount;我++){
        INT效用=的Integer.parseInt(s.next());        INT大小=的Integer.parseInt(s.next());        ArrayList的<整数GT;名单=新的ArrayList<整数GT;();        对于(INT J = 0; J<大小; J ++)
        {        list.add(s.nextInt());
        }        FileWriter的FW =新的FileWriter(file.getAbsoluteFile());
        BufferedWriter将体重=新的BufferedWriter(FW);        对于(INT K = 0; K<则为list.size() - 1; k ++){
            对于(INT J = K + 1; J< =则为list.size() - 1; J ++){
                    INT总和= list.get(K)+ list.get(J);                    如果(总和==效用){
                    的System.out.println((K + 1)+,+第(j + 1));
                    档案文件=新的文件(D:/A2.txt);
                    如果(!file.exists()){
                        file.createNewFile();
                    }                    bw.write(案例:+ I + - +(K + 1)+,+(J + 1)+可为+ +效用价值观是+ list.get(K)+, + list.get(J));
                    }            }
            }         bw.close();
        }
    S.CLOSE();}
}

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

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();

    }
    }

the line printed in the file is

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

解决方案

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天全站免登陆