输出到一个封闭的流? 我修正了我的程序,但问题是,在用DelDes替换所有空白空间之后,我必须把文本输出到一个已经关闭的文件。如何重新打开文件进行输出和输入 //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); } } } //姓名:李鹏飞 //程序文件:vouels.Java //用途:使用文件IO读取文件的输入并将此文本输出到新的文本文件 //输出时,所有空格将变为波浪形,每个元音都有一个计数(AEIOU) 导入java.util.Scanner//输入 导入java.io.File//木卫一 导入java.io.IOException//IO异常类 导入java.io.FileWriter//文件输出 导入java.io.FileReader//文件输入 导入java.io.FileNotFoundException//如果未找到文件,则类中未找到文件 公共类元音{//class 公共静态void main(字符串[]args){//main方法 try{//try块 FileReader poetry=新的FileReader(“poetry.txt”); FileWriter=新的FileWriter( “LI_ALLEN_Destint.txt”); INTA; 而((a=poetry.read())!=-1){ 牙医:写(a); System.out.print((char)a);//将文件打印到监视器 } 诗歌。关闭(); 牙医。关闭(); 扫描仪内嵌=新扫描仪(新文件( “LI_ALLEN_Destint.txt”); int numofewalsa=0;//A/E/I/O/U元音的计数 int numofuelse=0; int numofuelsi=0; int numofuelso=0; int numofuelsu=0; 而(infle.hasNext()){ 字符串语句=infle.next()/*(“\\S+”)*/; 对于(int i=0;i

输出到一个封闭的流? 我修正了我的程序,但问题是,在用DelDes替换所有空白空间之后,我必须把文本输出到一个已经关闭的文件。如何重新打开文件进行输出和输入 //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); } } } //姓名:李鹏飞 //程序文件:vouels.Java //用途:使用文件IO读取文件的输入并将此文本输出到新的文本文件 //输出时,所有空格将变为波浪形,每个元音都有一个计数(AEIOU) 导入java.util.Scanner//输入 导入java.io.File//木卫一 导入java.io.IOException//IO异常类 导入java.io.FileWriter//文件输出 导入java.io.FileReader//文件输入 导入java.io.FileNotFoundException//如果未找到文件,则类中未找到文件 公共类元音{//class 公共静态void main(字符串[]args){//main方法 try{//try块 FileReader poetry=新的FileReader(“poetry.txt”); FileWriter=新的FileWriter( “LI_ALLEN_Destint.txt”); INTA; 而((a=poetry.read())!=-1){ 牙医:写(a); System.out.print((char)a);//将文件打印到监视器 } 诗歌。关闭(); 牙医。关闭(); 扫描仪内嵌=新扫描仪(新文件( “LI_ALLEN_Destint.txt”); int numofewalsa=0;//A/E/I/O/U元音的计数 int numofuelse=0; int numofuelsi=0; int numofuelso=0; int numofuelsu=0; 而(infle.hasNext()){ 字符串语句=infle.next()/*(“\\S+”)*/; 对于(int i=0;i,java,Java,您没有关闭infle。首先关闭infle,然后您可以在平铺中再次打开它。 在Scanner tildes=new Scanner(…);line之前关闭它。为什么不在用正确的值填充流之后关闭它呢?我已尝试移动牙医。Close();在捕获异常之前的第二个while循环之后结束。出于某种原因,将该行移到末尾会禁用大量的if块,行中的元音根本不计算,所有值最终都为0。@Luiggimendozai如果关闭infle,则只允许它计算并完成对文件.Als上文本行的计算o如果我关闭了infle,它不会评估句

您没有关闭
infle
。首先关闭
infle
,然后您可以在
平铺中再次打开它。

Scanner tildes=new Scanner(…);
line之前关闭它。

为什么不在用正确的值填充流之后关闭它呢?我已尝试移动牙医。Close();在捕获异常之前的第二个while循环之后结束。出于某种原因,将该行移到末尾会禁用大量的if块,行中的元音根本不计算,所有值最终都为0。@Luiggimendozai如果关闭infle,则只允许它计算并完成对文件.Als上文本行的计算o如果我关闭了infle,它不会评估句子。我不明白你想说什么。@allenlistari如果我关闭infle,它不会向文件本身写入任何内容,那么就无法计算元音的数量。