简体   繁体   中英

What IOException do i use for FileWriter

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

I keep getting the error:

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

I'm trying to write into the file from strings from another file. I am taking info in from 2 different files and writing into one but, reading in from another. My nested for loop basically goes through and sees if the substring from the first file is a letter from the alphabet, and if it is it goes and adds it to the second file by using the write() method. It counts up how many chars at writes and then system out prints it. I just don't know what exception to add. Thank you so much any help is very appreciated!!

Your problem is this line:

FileWriter fw = new FileWriter(file2);

Constructing the FileWriter is throwing an IOException, and because it is outside your try-Catch block, it isn't being caught. I suggest you try addding it in the following way:

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

This turns your try-catch into a try-with-resources block which basically means once the code is block is finished the writers should automatically close

EDIT

If you assign the variables in the try-with-Resources like so:

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)) {

then you can remove all of these:

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

As the try-with-resources can automatically try and close anything within the (at the start to save you trying to do it manually)

You have unhandle exception here,Try implement try-catch for it

FileWriter fw = new FileWriter(file2);

can be changed to

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

and you can put these resource close in finally block of respective try-catch-finally. You will also need to check if not null

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM