如何从输入流重新打开文件

use*_*516 1 java file-io

我想重新打开一个文件.我在输入流中有一个文件.我尝试过使用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);
                    }

                }
Run Code Online (Sandbox Code Playgroud)

Pet*_*rey 6

一旦流,读取器,写入器,套接字或任何其他资源关闭,您就无法再次打开它.

如果要多次读取文件,则需要具有其文件名.