简体   繁体   English

管理 java 中的文件读取、保存、写入

[英]Managing Files in java Read,Save,Write

I would like to write a general method readfile() to a class to use it on the subclasses to read all lines.我想将通用方法 readfile() 写入 class 以在子类上使用它来读取所有行。

I have trouble with how to call methods from one class to another.我不知道如何从一个 class 调用方法到另一个。

First, is it better to make void readfile()?首先,使 void readfile() 更好吗? or return a File?或返回文件?

Secondly, which is the way to reused from other classes?其次,从其他类中重用的方式是什么?

Example of my code:我的代码示例:

public class Reader{

    Scanner myReader = new Scanner(myObj);
    public void readFile(){
        File myObj = new File("filename.txt");
          Scanner myReader = new Scanner(myObj);
          while (myReader.hasNextLine()) {
            String data = myReader.nextLine();
            System.out.println(data);
          }
    }

}

    public class ReadContentOfFile extends Reader{
        public List<String> parseFile{
           List<String> name = new ArrayList<>();
        //how to get lines? as I have them, from 
         another       method?
        //for example If I want to get the 
       words,separated, by comma

           return name;


        }

    }
public void Main(){
 }

For example for each line of a file, how I get each element.例如对于文件的每一行,我如何获取每个元素。 My problem is about how to get the data for another method, for another class.我的问题是关于如何获取另一种方法的数据,另一种 class。

First, is it better to make void readfile()?首先,使 void readfile() 更好吗? or return a File?或返回文件?

Depends on what you want to do with the output, since you are printing the contents to console then you don't have to return the file and void is OK.取决于你想用 output 做什么,因为你正在将内容打印到控制台,所以你不必返回文件并且 void 是可以的。 But if you wanted to use this file after you call the readFile method then you must return it or set it to class member.但是,如果您想在调用readFile方法后使用此文件,则必须将其返回或将其设置为 class 成员。

Secondly, which is the way to reused from other classes?其次,从其他类中重用的方式是什么? make your method static, so you can access it without creating an object since it's just a utility and object is not important here.制作你的方法 static,这样你就可以在不创建 object 的情况下访问它,因为它只是一个实用程序,而 object 在这里并不重要。

public static void readFile(){
//..
}

then do然后做

Reader.readFile()

Please, do not use Scanner to read a file, there are simpler and better options.请不要使用扫描仪读取文件,有更简单更好的选择。

First, a File itself it's nothing but a reference and it won't contain any content related to the file on system.首先, File本身只是一个参考,它不会包含与系统上的文件相关的任何内容。 So, you can consider to return a File object only if you may need some information from the file-system about privileges, existence, perform deleting actions or retrieving path information.因此,只有当您可能需要文件系统中有关权限、存在、执行删除操作或检索路径信息的一些信息时,您才可以考虑返回文件 object。

Usually when i write methods to read() or save() a file i make them void.通常,当我编写读取()或保存()文件的方法时,我会使它们无效。

About how do you read data and access it, in java there are a lot of possibilities.关于如何读取和访问数据,在 java 中有很多可能性。

I'll show you one pretty straightforward:我将向您展示一个非常简单的方法:

List<String> lines = Files.readAllLines(Paths.get("filename.txt"), StandardCharsets.UTF_8);

And that's it but there are more options.就是这样,但还有更多选择。

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

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