2012-11-15 3 views
0

샘플 코드는 다음과 같습니다 -두 번째 메서드에서 예외가 발생하지 않습니다. 그 이유는 무엇입니까?

import java.io.*; 
import java.util.List; 
import java.util.ArrayList; 

public class ListOfNumbers{ 
    private static int Size=10;    // this is a class variable- as there should be only 1 instance of the class. 
    private List<Integer> list; 

    public ListOfNumbers(){ 
    list = new ArrayList<Integer>(Size);  // an arraylist of size-10. 
    System.out.println("Populating the list"); 
    for(int i =0; i<Size; i++) 
    { 
    list.add(new Integer(i)); // is similar to using list.add(i); /* this involves auto-boxing*/ 
    } 
    System.out.println("List populated"); 
    } 

    public void writeList(File file){ 
    FileWriter out = null; 
    try{ 
    out = new FileWriter(file); 
    System.out.println("Start Writing to the File" + " " +file); 
    for(int i =0; i < Size; i++) 
    { 
    out.write("Value at" + i + "is:" +" " + (list.get(i)).intValue()); //is similar to writing list.get(i) /*this will return Integer, because of AB it will treated int*/ 
    out.write("\n"); 
    } 
    System.out.println("Contents written to the file"); 
    } 
    catch(FileNotFoundException fnfex){ 
    fnfex.printStackTrace(); 
    } 
    catch(IOException ioex){ 
    ioex.printStackTrace(); 
    } 
    finally{ 
    if(out != null) 
    { 
     try{ 
      out.close(); 
      } 

     catch(IOException ioex) 
     { 
     ioex.printStackTrace(); 
     } 
    } 

    else 
     System.out.println("Stream not open"); 

    } 
    } 

    public void readList(File file) 
    { 
    String pointer = null; 
    String [] value = null; 
    RandomAccessFile input = null; 
    System.out.println("Trying to read from file:" + " " + file); 
    try{ 
    System.out.println("In the try block"); 
    input= new RandomAccessFile(file,"r"); //should throw exception 
    System.out.println("Created a reference to the file"+ file); 
    while((pointer = input.readLine()) !=null) //should throw exception 
    { 
    System.out.println("In the while block"); 
    System.out.println(pointer); 
    value = pointer.split(": "); 
    list.add(Integer.parseInt(value[1])); 
    } 

    } 
    catch(FileNotFoundException fnfex) 
    { 
    fnfex.printStackTrace(); 
    } 
    catch(IOException ioex) 
    { 
    ioex.getMessage(); 
    } 

    } 

    public void displayList(){ 
    System.out.println("Displaying List elements"); 
// list.size(); 
    for(int i=0; i < list.size(); i++){ 
    System.out.println(list.get(i));  
    } 
    } 
    public static void main(String [] par) 
    { 
     ListOfNumbers obj = new ListOfNumbers(); 
     File file = new File("Output.txt"); 
     obj.writeList(file); 
     obj.readList(file); 
     obj.displayList(); 

    } 

} 

결과 : -

Populating the list 
List populated 
java.io.FileNotFoundException: Output.txt (Permission denied) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:209) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:160) 
    at java.io.FileWriter.<init>(FileWriter.java:90) 
    at ListOfNumbers.writeList(ListOfNumbers.java:22) 
    at ListOfNumbers.main(ListOfNumbers.java:97) 
Stream not open 
Trying to read from file: Output.txt 
In the try block 
Created a reference to the fileOutput.txt 
Displaying List elements 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

나는 예외를 얻을 그래서 Output.txt에 대한 사용 권한을 변경했습니다. 내 질문은 우리가 파일에 액세스하려고 할 때 readList() 메서드가 예외를 throw하지 않는 이유입니다. 즉 입력에 대한 참조를 지정하고 while 루프에서 사용하려고 할 때입니다.

+1

파일에 대한 사용 권한은 무엇입니까? 읽기가 많이 필요하지 않습니다 – jozefg

+0

어떻게 권한을 변경 했습니까? 파일이 현재 r, rw 또는 둘 중 하나입니까? –

+2

** 오직 **는'IOException' catch 블록에'ioex.getMessage();'을 가지고 있습니다. 그것은 아무것도하지 않습니다! 메시지를 인쇄하거나 FNF Exception에서와 같이 (더 나은) 스택 추적을 인쇄하십시오. –

답변

0

출력의 다양한 파일 권한에 따라 : -

1) -rw-RW ---- 1 개 루트 루트 0 11월 15일 17시 23분

[email protected]:/home/mount/Data/JAVA/practice/src$ java ListOfNumbers 
Populating the list 
List populated 
java.io.FileNotFoundException: Output.txt (Permission denied) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:209) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:160) 
    at java.io.FileWriter.<init>(FileWriter.java:90) 
    at ListOfNumbers.writeList(ListOfNumbers.java:22) 
    at ListOfNumbers.main(ListOfNumbers.java:97) 
Stream not open 
Trying to read from file: Output.txt 
In the try block 
java.io.FileNotFoundException: Output.txt (Permission denied) 
    at java.io.RandomAccessFile.open(Native Method) 
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:233) 
    at ListOfNumbers.readList(ListOfNumbers.java:64) 
    at ListOfNumbers.main(ListOfNumbers.java:98) 
Displaying List elements 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

2) -rw-과 Output.txt RW ---- 1 개 버퍼 버퍼 0 십일 15 17시 23분

과 Output.txt
[email protected]:/home/mount/Data/JAVA/practice/src$ java ListOfNumbers 
Populating the list 
List populated 
Start Writing to the File Output.txt 
Contents written to the file 
Trying to read from file: Output.txt 
In the try block 
Created a reference to the fileOutput.txt 
In the while block 
Value at0is: 0 
In the while block 
Value at1is: 1 
In the while block 
Value at2is: 2 
In the while block 
Value at3is: 3 
In the while block 
Value at4is: 4 
In the while block 
Value at5is: 5 
In the while block 
Value at6is: 6 
In the while block 
Value at7is: 7 
In the while block 
Value at8is: 8 
In the while block 
Value at9is: 9 
Displaying List elements 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

3) -rw-RW-r-- 같이 1 개 루트 루트 150 십일 16 5시 16분

과 Output.txt 012,343,340,973,
관련 문제