简体   繁体   English

使文本文件可用于Java中的所有方法

[英]Making a text file available to all methods in Java

I have a simple single Java file, that has main method and a bunch of other methods. 我有一个简单的单个Java文件,其中包含main方法和许多其他方法。 I'd like to open a text file in my main method but keep it open and append to it in my other methods in my java code. 我想在我的main方法中打开一个文本文件,但保持打开状态并在Java代码中的其他方法中附加到该文件。 what is the best way of doing it in Java. 用Java做到最好的方法是什么?

Take a look at the FileWriter Class in java.io. 看一下java.io中的FileWriter类。

private FileWriter _writer;

public static void main(String args[])
{
    _writer = new FileWriter(filePath, true);
}

The filepath is the name of your file, true is to append and not rewrite your file everytime. filepath是文件的名称,true表示每次都添加而不是重写文件。 This writer would stay open until you close it. 该作家将保持打开状态,直到您关闭它。

_writer.close();

I will point out that this is probably not the best option. 我将指出这可能不是最佳选择。 Since you can append at anytime to a file, there is no reason to leave your stream open. 由于您可以随时附加到文件,因此没有理由让流保持打开状态。 You can simply recreate it with the append variable as true. 您可以简单地使用append变量将其重新创建为true。

A write method might fit you better, so that you can also put it on it's own thread some day. write方法可能更适合您,因此您有一天也可以将其放在自己的线程中。

public static void write(String output)
{
    FileWriter writer = new FileWriter(filePath, true);
    writer.write(output);
    writer.close();
}

You need what in object oriented programming is called an "Instance variable" 您需要在面向对象编程中将其称为“实例变量”

This variable is accessible from methods in the same object and you don't have to pass it around. 可以从同一对象中的方法访问此变量,而不必传递它。

So your class could be defined as: 因此,您的课程可以定义为:

class MyClass {
    // This is your instance variable 
    private PrintWriter output;

    public void methodOne() {
        output.println("One");
    }

    public void methodTwo() {
        output.println("Two");
    }

    public void methodThree() {
        output.println("Three");
    }

    // you get the idea 

    public void close(){
       output.close();
    }
}

That way you can create an object of MyClass and initialize your instance variable and use it in all the methods. 这样,您可以创建MyClass的对象并初始化您的实例变量,然后在所有方法中使用它。

This could be the main method: 这可能是主要方法:

 public static void main( String [] args  ) {
      MyClass object = new MyClass();
      // add another method to initialize the instance variable 
      object.useFile("/Users/yourname/yourFile.txt"); 
      // useFile defined as: internally initialized as: 
      // output = new PrintStream( new FileOutputStream( fileName ));
      object.methodOne();
      object.methodTwo();
      object.methodThree(),
      object.close();
  }

The idea, is to have a private attribute which is used in the rest of the methods, so you don't have to pass the reference all over the place. 这个想法是要有一个私有属性,该私有属性将在其余方法中使用,因此您不必在各处传递引用。

If you have different classes trying to access the same attribute, maybe you could move those methods to this class. 如果您有尝试访问同一属性的不同类,则可以将这些方法移至此类。

What you are seeking is a Writer, an object that lets methods write into it (or PrintWriter, which lets you print line-by-line). 您正在寻找的是Writer,它是一个允许方法写入其中的对象(或PrintWriter,它允许您逐行打印)。 You can create Writers from various output sources, including files with FileWriter. 您可以从各种输出源创建Writer,包括使用FileWriter的文件。

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

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