Javaでファイルに書き込む際の問題

user3717634

ファイルに書き込もうとしていますが、テキストファイルの各行に入る行を書き込む行でエラーが発生しました。ヘルプがわからない場合は、ライターコードをいただければ幸いです。

 public static void stuIDWrite() throws IOException
 {
     Writer writer = null;

 try {
     writer = new BufferedWriter(new OutputStreamWriter(
           new FileOutputStream("Res/stuIDSorted.txt")));
 } catch (IOException ex) {
   // report
 } finally {
    try {writer.close();} catch (Exception ex) {}
 }
 int i = 0;

    while (i <= stuArrayIdSort.length + 1)
    {
        ln = stuArrayIdSort[i].getStuLastName();    
        fn = stuArrayIdSort[i].getStuFirstName();
        pn = stuArrayIdSort[i].getStuFirstName();
        id = stuArrayIdSort[i].getStuId();
        ft = stuArrayIdSort[i].getFTime();
        phn =stuArrayIdSort[i].getPhoneNum();
        lj = stuArrayIdSort[i].getLovJava();
        con = stuArrayIdSort[i].getCont();
            writer.write(ln + "," + fn + "," + pn  + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
            writer.close();
            i++;    
    }

完全なコード

import java.io.*;
import java.util.*;
public class StudentMain {
    /**
     * @param args
     */

        //array and sorting variables
     public static studentConstructor[] stuArrayOrig = new studentConstructor[23];
     private static studentConstructor[] stuArrayIdSort = new studentConstructor[23];
     private static studentConstructor[] stuArrayNameSort = new studentConstructor[23];
     private static int lineCount = 0;
     private static int nElms = 0;

     //writer


     //studentConstructor variables 
        public static String fn; //First Name
        public static String ln; //Last Name
        public static String pn; //Preferred Name
        public static int id;     //Student Id Number
        public static boolean ft;//Full-time Boolean
        public static int phn;   //Student Phone Number
        public static boolean lj;//Loving java Boolean
        public static String con;//Continuing 


        File idSort = new File("stuListSortID.txt");


     public static void StuRead()
     {

        Scanner inFile = null;

         try
         {
             inFile = new Scanner
                    (new FileReader("Res/students.txt"));
         }
         catch (FileNotFoundException e)
         {
             // TODO Auto-generated catch block
             System.out.println("File Not Found");
             e.printStackTrace();
         }

            while (inFile.hasNextLine()){
                        inFile.useDelimiter(",|\\n"); //breaks the lines into single info

                        ln = inFile.next();
                            System.out.println(ln);

                        fn = inFile.next();
                            System.out.println(fn);

                        pn = inFile.next();
                            System.out.println(pn);

                        id = inFile.nextInt();
                            System.out.println(id);

                        ft = inFile.nextBoolean();
                            System.out.println(ft);

                        phn = inFile.nextInt();
                            System.out.println(phn);

                        lj = inFile.nextBoolean();
                            System.out.println(lj);

                        con = inFile.next();
                            System.out.println(con);

                            studentConstructor st = new studentConstructor(ln, fn, pn, id, ft, phn, lj, con);
                            stuArrayOrig[lineCount] = st;
                            inFile.nextLine();
                            System.out.println(stuArrayOrig[lineCount]);


                        lineCount++;
                    }
            //setting info into other arrays
            stuArrayIdSort = stuArrayOrig;
            stuArrayNameSort = stuArrayOrig;            

            System.out.println("orig array length" + stuArrayOrig.length);
            System.out.println("id array length" + stuArrayIdSort.length);
            System.out.println("name array length" + stuArrayNameSort.length);
            System.out.println("number of file lines" + lineCount);




            inFile.close(); 


    }    

     public static void stuIdSort()
     {
         studentConstructor temp;
         boolean sorted = false;

         while (sorted == false)
         {  sorted=true;
            for (int i=0; i<stuArrayIdSort.length-1 ; i++)
            {
                if(stuArrayIdSort[i].getStuId() > stuArrayIdSort[i+1].getStuId())
                {
                    temp = stuArrayIdSort[i+1];
                    stuArrayIdSort[i+1] = stuArrayIdSort[i];
                    stuArrayIdSort[i] = temp;
                    sorted=false;

                }
            }
        }
         for(int i=0; i<stuArrayIdSort.length; i++)
         {
             int getSC = stuArrayIdSort[i].studentId;
             System.out.println("number of swaps " + i+1 +" " +getSC);
         }
     }

    //stuArrayIdSort[i].getStuLastName(),stuArrayIdSort[i].getStuFirstName(),stuArrayIdSort[i].getPrefName(),stuArrayIdSort[i].getStuId(),stuArrayIdSort[i].getFTime(),stuArrayIdSort[i].getPhoneNum(),stuArrayIdSort[i].getLovJava(),stuArrayIdSort[i].getCont()
     public static void stuIDWrite() throws IOException
     {
         Writer writer = null;

         try {
             writer = new BufferedWriter(new OutputStreamWriter(
                   new FileOutputStream("Res/stuIDSorted.txt")));
         } catch (IOException ex) {
           // report
         } finally {
            try {writer.close();} catch (Exception ex) {}
         }
         int i = 0;

            while (i <= stuArrayIdSort.length + 1)
            {
                ln = stuArrayIdSort[i].getStuLastName();    
                fn = stuArrayIdSort[i].getStuFirstName();
                pn = stuArrayIdSort[i].getStuFirstName();
                id = stuArrayIdSort[i].getStuId();
                ft = stuArrayIdSort[i].getFTime();
                phn =stuArrayIdSort[i].getPhoneNum();
                lj = stuArrayIdSort[i].getLovJava();
                con = stuArrayIdSort[i].getCont();
                    writer.write(ln + "," + fn + "," + pn  + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
                    writer.close();
                    i++;    
            }
     }

     public static void stuNameSort()
     {

     }

     public static void stuNameWrire()
     {

     }
}
//lastName, firstName, perName, studentId, fulltime,
ディーン・レイタースドルフ

わかりました、これがあなたがすべきことです:

何が起こっているのかというと、実際に何かを実行する前に閉じているということです。それでは、finally句をすべての最後に移動しましょう。

public static void stuIDWrite() throws IOException
 {
     Writer writer = null;

 try {
     writer = new BufferedWriter(new OutputStreamWriter(
       new FileOutputStream("Res/stuIDSorted.txt")));

 int i = 0;

while (i <= stuArrayIdSort.length + 1)
{
    ln = stuArrayIdSort[i].getStuLastName();    
    fn = stuArrayIdSort[i].getStuFirstName();
    pn = stuArrayIdSort[i].getStuFirstName();
    id = stuArrayIdSort[i].getStuId();
    ft = stuArrayIdSort[i].getFTime();
    phn =stuArrayIdSort[i].getPhoneNum();
    lj = stuArrayIdSort[i].getLovJava();
    con = stuArrayIdSort[i].getCont();
        writer.write(ln + "," + fn + "," + pn  + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
        i++;    
}
} catch (IOException ex) {
   // report
  } finally {
try {writer.close();} catch (Exception ex) {}
 }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Pythonで辞書をYAMLファイルに書き込む際の問題

分類Dev

Pythonでファイルに複数行を書き込む際の問題

分類Dev

特定の行のtxtファイルに書き込む際の問題

分類Dev

結果をcsvファイルに書き込む際の問題

分類Dev

PHPを使用してLinuxにファイルを書き込む際の問題

分類Dev

スクレイプスパイダーでcsvファイルに書き込む際の問題

分類Dev

Javaプログラムからサーバー上のtxtファイルに書き込む際の問題

分類Dev

Pythonがバイトをファイルに書き込む際の誤った順序の問題

分類Dev

テキストファイルを辞書に読み込む際の問題

分類Dev

スクリプトの出力をファイルに書き込む際の問題

分類Dev

Excelファイルにデータのリストを書き込む際の問題

分類Dev

dup2とprintfを使用してファイルに書き込む際の問題

分類Dev

14.04でRを使用して.csvファイルを書き込む際の問題

分類Dev

Python 2:Excelファイルに書き込むときのASCIIの問題

分類Dev

C:\ドライブ権限の問題でファイルを書き込む

分類Dev

Powershell出力をcsvファイルに書き込むのに問題がある

分類Dev

Python 3.6 で csv ファイルに複数の行を書き込むときに問題が発生する

分類Dev

FTPでダウンロードした後、ファイルをディスクに書き込む際の問題

分類Dev

Java:DOMをXMLファイルに書き込む(フォーマットの問題)

分類Dev

read()を使用してファイルの内容を画面およびCの他のファイルに書き込む際の問題

分類Dev

UbuntuでPHPを使用してファイルに書き込むのに問題がある

分類Dev

解決済みの問題をファイルに書き込むことは可能ですか?

分類Dev

Javaでの.txtファイルへの書き込みの問題

分類Dev

C#のテキストファイルに文字列データのバイト形式を書き込む際の問題

分類Dev

Javaファイルの書き込みの問題

分類Dev

Java:XMLファイルの問題への書き込み

分類Dev

node.js:システムコールを使用して/ tmpディレクトリにファイルを書き込む際の問題

分類Dev

ファイルJavaへの書き込みに関する問題

分類Dev

Webサーバー(flask、apache、wsgi)にファイルを書き込む際のアクセス許可の問題

Related 関連記事

  1. 1

    Pythonで辞書をYAMLファイルに書き込む際の問題

  2. 2

    Pythonでファイルに複数行を書き込む際の問題

  3. 3

    特定の行のtxtファイルに書き込む際の問題

  4. 4

    結果をcsvファイルに書き込む際の問題

  5. 5

    PHPを使用してLinuxにファイルを書き込む際の問題

  6. 6

    スクレイプスパイダーでcsvファイルに書き込む際の問題

  7. 7

    Javaプログラムからサーバー上のtxtファイルに書き込む際の問題

  8. 8

    Pythonがバイトをファイルに書き込む際の誤った順序の問題

  9. 9

    テキストファイルを辞書に読み込む際の問題

  10. 10

    スクリプトの出力をファイルに書き込む際の問題

  11. 11

    Excelファイルにデータのリストを書き込む際の問題

  12. 12

    dup2とprintfを使用してファイルに書き込む際の問題

  13. 13

    14.04でRを使用して.csvファイルを書き込む際の問題

  14. 14

    Python 2:Excelファイルに書き込むときのASCIIの問題

  15. 15

    C:\ドライブ権限の問題でファイルを書き込む

  16. 16

    Powershell出力をcsvファイルに書き込むのに問題がある

  17. 17

    Python 3.6 で csv ファイルに複数の行を書き込むときに問題が発生する

  18. 18

    FTPでダウンロードした後、ファイルをディスクに書き込む際の問題

  19. 19

    Java:DOMをXMLファイルに書き込む(フォーマットの問題)

  20. 20

    read()を使用してファイルの内容を画面およびCの他のファイルに書き込む際の問題

  21. 21

    UbuntuでPHPを使用してファイルに書き込むのに問題がある

  22. 22

    解決済みの問題をファイルに書き込むことは可能ですか?

  23. 23

    Javaでの.txtファイルへの書き込みの問題

  24. 24

    C#のテキストファイルに文字列データのバイト形式を書き込む際の問題

  25. 25

    Javaファイルの書き込みの問題

  26. 26

    Java:XMLファイルの問題への書き込み

  27. 27

    node.js:システムコールを使用して/ tmpディレクトリにファイルを書き込む際の問題

  28. 28

    ファイルJavaへの書き込みに関する問題

  29. 29

    Webサーバー(flask、apache、wsgi)にファイルを書き込む際のアクセス許可の問題

ホットタグ

アーカイブ