2012-11-15 31 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()方法不会抛出异常,即为输入分配引用,以及何时尝试在while循环中使用它。

+1

该文件的权限是什么?读取它并不需要太多 – jozefg

+0

您是如何更改权限的?文件现在是r,rw还是两者都不? –

+2

只有你**在'IOException' catch块中拥有'ioex.getMessage();'。那什么都不做!或者*打印*信息,或者(更好地)打印堆栈跟踪,就像使用FNF异常一样。 –

回答

0

输出的根据各种文件的权限: -

1)-rw-RW ---- 1根根0 11月15日17时23 Output.txt的

[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- RW ---- 1个缓冲器缓冲器0 11月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年11月16 5时16 Output.txt的

[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 
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 
相关问题