Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将输出保存到名为;output.txt“;_Java_File Io_Io - Fatal编程技术网

Java 如何将输出保存到名为;output.txt“;

Java 如何将输出保存到名为;output.txt“;,java,file-io,io,Java,File Io,Io,我从来没有将输出保存到文件中,我不知道如何做,所以如果有人能帮助我,那就太棒了。如果需要,代码将发布在下面 代码: import java.util.*; import java.io.*; public class Wordcount { public static void main(String[] args) { int vowels=0; int punctuation=0; int sentences=0;

我从来没有将输出保存到文件中,我不知道如何做,所以如果有人能帮助我,那就太棒了。如果需要,代码将发布在下面

代码:

import java.util.*;
import java.io.*;

public class Wordcount {


    public static void main(String[] args) {
        int vowels=0;
        int punctuation=0;
        int sentences=0;
        int words=0;
        int lines=0;
        int alphaNumeric=0;

       try{
       //creates scanner that reads file name    
       Scanner input = new Scanner(System.in);
       System.out.println("Enter file name: ");

       //creates file object of file inputted above
       File file = new File(input.nextLine());
       //if statement for when the file is empty
       if (file.length()==0){
           System.out.println("The input file is empty.");
           System.exit(1);
       }
       //scanner made for reading words of file
       Scanner fileReader = new Scanner(file);
       //new scanner is created to specifically assist in counting the 
       //number of lines in the file
       Scanner lineCounter = new Scanner(file);
       //while loop to count the lines
       while (lineCounter.hasNextLine()) {
           lines++;
           lineCounter.nextLine();
       }

       //while loop that is created to examine each word of the file 
       while(fileReader.hasNext()){
           String word = fileReader.next();
           //for loop which examines each character of each word in the file
           //characters are stored in temp variable ch
           for (int i=0; i<word.length();i++){
               char ch= word.charAt(i);
               //checks for vowels
               if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
                   vowels ++;
               //checks for sentences
               if((ch=='!'||ch=='.'||ch=='?'))
                   sentences ++;
               //checks for alphanumerical character
               if(Character.isLetterOrDigit(ch))
                   alphaNumeric ++;
               //checks for punctuation
               switch(ch){
                   case ',': case '[': case ']': case ':': case '`': case '-':
                   case '!': case '_': case '(': case ')': case '.': case '?':
                   case '"': case ';':
                    punctuation ++;
                    break;

               }    
           }
           //increments the words and then goes back into the loop
           words ++;

       }
       //output
        System.out.println("The number of words in the file name: " + words);
        System.out.println("The number of lines in the file name: " + lines);
        System.out.println("The number of alphanumeric characters "
                + "in the file name: " + alphaNumeric);
        System.out.println("The number of sentences in the file name: "
                + sentences);
        System.out.println("The number of vowels in the file name: " + vowels);
        System.out.println("The number of punctuations in the file name: " 
                + punctuation);

       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
    }
}
import java.util.*;
导入java.io.*;
公共类字数{
公共静态void main(字符串[]args){
int元音=0;
int标点=0;
整句=0;
int字=0;
int行=0;
int字母数字=0;
试一试{
//创建读取文件名的扫描仪
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入文件名:”);
//创建上面输入的文件的文件对象
File File=新文件(input.nextLine());
//文件为空时的if语句
如果(file.length()==0){
System.out.println(“输入文件为空”);
系统出口(1);
}
//扫描仪用于读取文件中的文字
Scanner fileReader=新扫描仪(文件);
//创建新扫描仪是为了专门帮助计算
//文件中的行数
扫描仪lineCounter=新扫描仪(文件);
//同时循环计算行数
while(lineCounter.hasNextLine()){
行++;
lineCounter.nextLine();
}
//while循环,用于检查文件的每个字
while(fileReader.hasNext()){
String word=fileReader.next();
//for循环,它检查文件中每个单词的每个字符
//字符存储在temp变量ch中

对于(int i=0;iUnix系统:
JavaMyClass>output.txt

请参阅java教程中的内容。请在询问之前进行研究。