3

I want to re-open a file. I have a file in Input stream. I have tried using Scanner and using BufferedReader. But I am unable to open the file again after it is closed using close() method. Please help how to open a file again. I have written the below code:

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

                }
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
user179516
  • 337
  • 4
  • 11
  • 20

3 Answers3

7

Once a stream, reader, writer, socket or any other resource has closed, you can't open it again.

If you want to read a file more than once, you need to have its file name.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Just to add to your answer, once the stream wrapper is closed, it also closes all the underlying streams are closed too. – gigadot Sep 10 '12 at 08:38
  • To add to Peter's answer: "If you want to read a file more than once, you need to have its file name." In other words, you have to re-assign `getAttachstream()` to `filename1` again, **and** you have to re-assign `new Scanner(filename1)` to `inputStream1` again. Do these two things just after you've parsed a file once (more), and you are back to the beginning of the file. – Andrei Nov 20 '13 at 09:47
1

I assume you mean to reopen the InputStream you get from getAttachstream (even it is not shown nowhere that it comes from a file).

The only option would be for getAttachstream to return a class that implement such method. Keep in mind that even FileInputStream does not offer such option. And, even if you find the concrete class and it happens to have such a method, as the definition of the method returns an InputStream you can't be sure that it will always return that same class (or even that in all circunstance that will be the class returned).

The only option would be using the original inputStream and write it into a temporary file or a ByteArrayOutputStream (if the file/s are small enough to not use too much memory), so you can access the data several times.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

You can use scanner to count rows Scanner.hasNextLine()

Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

    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();
    }
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72