繁体   English   中英

需要帮助在java中写入输入/输出文件

[英]Need help writing to input/output file in java

java新手,需要一些帮助。 我正在处理2个文件。 一个定义了一个类及其所有方法,另一个构造了该类的实例。 我的项目要求我们将类方法的输出写入文件。 我的问题是如何让我的“类定义”文件中的方法写入与“main方法”中定义的文件相同的文件? 我目前正在使用System.out.print ,我知道这是不正确的。 //文件#1

public class JessiahP3 {
    boolean  isPlaying =  false;
    int strings  = 1;
    boolean isTuned = false;
    public String instrumentName;


    //is tuned
    public void tune() {
        if(isTuned == false)
            isTuned = true;
        else
            isTuned = false;
    }

    //instrument name
    public void setInstrumentName(String insName){
        instrumentName = insName;
    }

    //get instrument name
    public String getInstrumentName(){
        return instrumentName;
    }

    private Boolean getTuned(){
        return isTuned;
    }

    public void playInstrument(){
        isPlaying = true;
        System.out.println("The instrument is now playing.");
    }

    public void stopInstrument( ){
        isPlaying = false;
        System.out.print("The instrument has stopped playing.");
    }

    public void setString(int newString){
        if (newString >=  1 && newString <= 6)    
            strings = newString;
        else
            System.out.print("You have exeeded the maximum number of strings.");
    }

    public void stringUp(){
        if (strings < 10)
            strings++;
    }

    public void stringdown(){
        if (strings > 1)
            strings--;
    }

    public String[] getStringNames(String[] stringNames) {
        for (String i:stringNames){
            System.out.println(i);
        }
        return stringNames;
    }
}

文件#2

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

public class JessiahP3Test {

    public static void main(String[] args)throws Exception {
        //declare new input/ output file
        java.io.File newFile = new java.io.File("Jessiahp4.txt");
        //java.io.File newFile = new java.io.File(args[0]);

        //check to see if file exist
        if (newFile.exists()) {
            //delete existing file
            newFile.delete();
        }

        //create a file
        java.io.PrintWriter output = new java.io.PrintWriter(newFile);

        //instance 1 of Instrument = Guitar    
        JessiahP3 guitar = new JessiahP3();
        guitar.setInstrumentName("Guitar");
        guitar.setString(3);
        output.println("Your current string number is " + guitar.strings);
        String[] stringNames = {"Abe", "Blue", "Dream"};
        guitar.getStringNames(stringNames);
        output.print("Tuning " + guitar.getInstrumentName());
        guitar.tune();
        guitar.playInstrument();
        //output.print("The" + guitar.getInstrumentName() + "is playing");
        guitar.stopInstrument();


        //close i/o file
        output.close();
    }
}

将私有PrintWriter属性添加到第一个类,并定义一个setPrintWriter()方法,在实例化第一个类并创建PrintWriter之后,从第二个类调用该方法。 之后,您可以更改playInstrument()以使用它而不是System.out

注意:所有.java文件(接口除外)都可以归类为“类定义文件”,因为Java中的类外部不能存在任何代码。

因为您输出的大多数内容看起来像调试语句,所以还有第二种方法可以实现它。 您可以在第一个文件中使用System.setOut(PrintStream printStream),并将其传递给PrintStream(将PrintWriter更改为PrintStream,它与您使用它的方式相同)。 然后,在您的其他文件中,System.out.println()将打印到文件而不是标准输出。

使用此方法,您不必更改第一个文件。 实质上,这允许您将所有输出重定向到文件而不是命令行,这对GUI应用程序很有用。

所以在第二个文件中,更改:

java.io.PrintWriter output = new java.io.PrintWriter(newFile);

System.setOut(new PrintStream(newFile));

并改变:

output.println("Your current string number is " + guitar.strings);
...
output.print("Tuning " + guitar.getInstrumentName());

System.out.println("Your current string number is " + guitar.strings);
...
System.out.print("Tuning " + guitar.getInstrumentName());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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