我对FileWriter使用什么IOException

时间:2014-05-19 01:19:09

标签: java file exception try-catch ioexception

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;

/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class Writer {

public static void main(String[] args) {

File file = new File("C:\\Users\\McIntosh\\Documents\\APCS_Contest\\randomGenProb1.txt");
File file2 = new File("C:\\Users\\McIntosh\\Documents\\APCS_Contest\\Letters.txt");
FileWriter fw = new FileWriter(file2);
BufferedWriter bw = new BufferedWriter(fw);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try 
{
fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

int count = 0;
String str;
char temp;
String abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] list = abc.toCharArray();
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) 
{
  str = dis.readLine();
  for(int x=0;x<str.length()-1;x++)
  {
    for(int y=0;y<list.length;y++)
    {
      temp = str.charAt(x);
      if(temp==(list[y]))
      {
        bw.write(temp);
        count++;
      }
    }
  }
}
System.out.println(count+" Chars found, and added");
System.out.println("Finished! Chars added to file: " + file2);

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
bw.flush();
bw.close();
fw.close();

} 
 catch (FileNotFoundException e) 
  {
   e.printStackTrace();
  } 
 catch (IOException e) 
  {
   e.printStackTrace();
  }
}
} 

我一直收到错误:

Error: unreported exception java.io.IOException; must be caught or declared to be     thrown

我正在尝试从另一个文件的字符串写入文件。我从2个不同的文件中获取信息并写入一个但是从另一个文件中读取。我的嵌套for循环基本上是通过并查看来自第一个文件的子字符串是否是字母表中的字母,如果它是,它将使用write()方法将其添加到第二个文件。它计算写入时有多少个字符,然后系统输出打印它。我只是不知道要添加什么异常。非常感谢你们的帮助!

2 个答案:

答案 0 :(得分:1)

你的问题就在这一行:

FileWriter fw = new FileWriter(file2);

构造FileWriter会抛出一个IOException,因为它在try-Catch块之外,所以它没有被捕获。我建议你尝试按以下方式添加它:

try(FileWriter fw = new FileWriter(file2); BufferedWriter bw = new BufferedWriter(fw);) 
//and then the rest of your try catch block

这会将你的try-catch变成一个try-with-resources块,这基本上意味着一旦代码被块完成,编写者应该自动关闭

修改

如果您在try-with-Resources中分配变量,如下所示:

try(FileWriter fw = new FileWriter(file2); BufferedWriter bw = new BufferedWriter(fw);
    FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis)) {

然后你可以删除所有这些:

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
bw.flush();
bw.close();

由于try-with-resources可以自动尝试并关闭(在开始时保存你尝试手动执行的任何内容)

答案 1 :(得分:0)

你在这里有非处理异常,请尝试实现try-catch

FileWriter fw = new FileWriter(file2);

可以更改为

  FileWriter fw=null;
     try {
         fw = new FileWriter(file2);
     }catch(IOException e){
         e.printStackTrace();
     }

你可以把这些资源放在各自try-catch-finally的finally块中。您还需要检查是否为空

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
bw.flush();
bw.close();
fw.close();