2013-11-05 60 views
0

所以我修复了我的程序,但问题是,在用替代字符替换所有空格后,我必须将文本输出到已关闭的文件。我将如何重新打开输出文件并输入内容?输出到封闭的流?

//Name: Allen Li 
//Program file: Vowels.Java 
//Purpose: Using File IO, read a file's input and output this text to a new text file 
//When outputting, all blank spaces will be changed to tildes and there will be a count of each vowel(AEIOU) 


import java.util.Scanner; //input 
import java.io.File; //IO 
import java.io.IOException; //IO exception class 
import java.io.FileWriter; //file output 
import java.io.FileReader; //file input 
import java.io.FileNotFoundException; //if file isnt found, file not found class 

public class Vowels { //class 
public static void main(String[] args) { //main method 

    try { //try block 
    FileReader poetry = new FileReader("poetry.txt"); 
    FileWriter dentist = new FileWriter( 
    "LI_ALLEN_dentist.txt"); 

    int a; 
    while ((a = poetry.read()) != -1) { 
    dentist.write(a); 
    System.out.print((char) a); //print the file to the monitor 
    } 

    poetry.close(); 
    dentist.close(); 

    Scanner inFile = new Scanner(new File( 
    "LI_ALLEN_dentist.txt")); 

    int numOfVowelsA = 0; //count #s of A/E/I/O/U vowels 
    int numOfVowelsE = 0; 
    int numOfVowelsI = 0; 
    int numOfVowelsO = 0; 
    int numOfVowelsU = 0; 

    while (inFile.hasNext()) { 
    String sentence = inFile.next() /* ("\\S+") */; 

    for (int i = 0; i <= sentence.length() - 1; i++) { 
    if (sentence.toLowerCase().charAt(i) == 'a') { 
     numOfVowelsA++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'e') { 
     numOfVowelsE++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'i') { 
     numOfVowelsI++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'o') { 
     numOfVowelsO++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'u') { 
     numOfVowelsU++; 
    } 
    } 
    } 
    System.out.println(); 

    System.out.println("There are " + numOfVowelsA 
    + " A vowels in this file of text"); 
    System.out.println("There are " + numOfVowelsE 
    + " E vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsI 
    + " I vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsO 
    + " O vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsU 
    + " U vowels in this file of text. "); 



    Scanner tildes = new Scanner(new File( 
    "LI_ALLEN_dentist.txt")); 
    while (tildes.hasNext()) { 
    String replace = tildes.nextLine(); 
    replace = replace.replaceAll(" ", "~"); 
    System.out.println(); 
    System.out.print(replace); 

    } 

    } catch (FileNotFoundException i) { 
    System.out.println("The file you are trying to use as input is not found. " + i); 
    } catch (IOException i) { 
    System.out.println("There is an issue with the input or output file. " + i); 
    } 
} 
} 
+0

为什么不在填充正确值后关闭流? –

+0

我试过移动dentist.close();在捕获异常之前的第二个while循环之后结束。出于某种原因,将该行以某种方式移动到末尾会以某种方式禁用大量的块,并且行中的元音根本无法计数,并且所有值都会最终为0. @LuiggiMendoza – allenlistar

回答

0

您没有关闭​​。先关闭​​,然后您可以在tildes中再次打开它。 在Scanner tildes = new Scanner(...);之前关闭它。

+0

如果关闭inFile,只允许它评估并完成评估文件中的文本行。另外,如果我已关闭inFIle,它不评估句子。 – allenlistar

+0

我不明白你想说什么.. @allenlistar –

+0

如果我关闭inFile它不会写任何东西到文件本身,那么没有办法来计算元音的数量。 – allenlistar