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

标签 java io

Java 新手,需要一些帮助。我正在处理 2 个文件。一个定义一个类及其所有方法,另一个构造该类的实例。我的项目要求我们将类方法的输出写入文件。我的问题是如何将“类定义”文件中的方法写入与“主方法”中定义的文件相同的文件?我目前正在使用 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();
    }
}

最佳答案

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

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

关于java - 需要帮助在 java 中写入输入/输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735530/

相关文章:

java - 为什么 getCanonicalPath 会抛出已检查的异常而 getAbsolutePath 不会?

c# - 制作井字游戏的领域模型

java - 如何将按钮绑定(bind)到另一个 xml 文件?

java - Fresco 未更新图像

java - 如何将 java.lang.Appendable 包装到 java.io.Writer 中?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 将数组的内容添加到 JFrame、添加 JPanel、添加 JScroll

java - Android Studio 2.0 - NoSuchAlgorithmException : SHA256WITHDSA Signature not available

c - C标准库中使用内存流的目的是什么?

java - 从java文件中读取每个字符串文本