1

ファイルを再度開きたい。入力ストリームにファイルがあります。Scanner と BufferedReader を使用してみました。しかし、close() メソッドを使用して閉じた後、ファイルを再度開くことができません。ファイルを再度開く方法を教えてください。以下のコードを書きました。

InputStream filename = getAttachstream();

        int rows =0 ;

        BufferedReader br= new BufferedReader(new InputStreamReader(filename));
        String strLine = "";
          try {
            while( (strLine = br.readLine()) != null) {
                rows++;
              }
            //br.reset();
            br.close();
            //br.reset();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
if(rows>0){
            InputStream filename1 = getAttachstream();
            Scanner inputStream1 = new Scanner(filename1);
                for (int rowIncr = 1; inputStream1.hasNext(); rowIncr++) {

                String data;
                try {
                    data = br.readLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String [] values = data.split(",");
                String curRowPartNumber = values[0];
                String curRowQuantity =   values[1];
                if(rowIncr == 1)
                {
                    if((values[0]==null || values[0].trim().length()<=0)
                            || (values[1]==null || values[1].trim().length()<=0)
                            || (values[2] != "") || !"Part Number".equalsIgnoreCase(values[0].trim())
                            || !"Quantity".equalsIgnoreCase(values[1].trim())){
                        System.out.println("Invalid Excel sheet data");
                        throw new ECApplicationException(ECMessage._ERR_CMD_INVALID_DATAFORMAT, CLASSNAME,methodName);
                    }

                }
4

3 に答える 3

6

ストリーム、リーダー、ライター、ソケット、またはその他のリソースが閉じられると、再び開くことはできません。

ファイルを複数回読み取りたい場合は、そのファイル名が必要です。

于 2012-09-10T08:31:10.287 に答える
0

スキャナーを使用して行をカウントできますScanner.hasNextLine()

このスキャナの入力に別の行がある場合は true を返します。このメソッドは、入力の待機中にブロックされる場合があります。スキャナは入力を超えて進みません。

    File file = new File("C:/test.txt");
    File file1 = new File("C:/test1.txt");

    Scanner scanner;
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        FileInputStream fileInputStream1 = new FileInputStream(file1);
        scanner = new Scanner(fileInputStream);
        int count = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            count++;
        }
        scanner.close();
        System.out.println("File 1 Count:" + count);
        scanner = new Scanner(fileInputStream1);
        count = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            count++;
        }
        System.out.println("File 2 Count:" + count);
        scanner.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-09-10T08:34:05.240 に答える