如何在java程序启动时加载文件

时间:2014-06-23 13:23:51

标签: java drawing draw

我创建了一个绘图管理器,它从txt文件获取坐标,并使用一些函数用它们绘制几个形状。我的问题是如何让程序自动加载这种文件?换句话说,如何让一个程序在启动时加载一个txt文件,这样应用程序就会从屏幕上的形状开始?

1 个答案:

答案 0 :(得分:0)

这取决于您如何阅读文件,文件的位置以及处理数据的方式。但是每个Java-Programm都是通过调用一些public static void main(String[] args)方法开始的。这就是你读取文件的地方,如果你想在加载实际窗口之前完成它。

 public static void main(String[] args) {
    Scanner scanner = new Scanner("coordinates.txt");
    ArrayList<String> lines = new ArrayList<>(); // just something to hold the lines of the text file seperately
    while (scanner.hasNextLine()){
        lines.add(scanner.nextLine());          // read all lines of the text file
    }

    new MyMainApplicationWindow(lines);        // I don't know how you process the data in your application, so I just pass the list of lines with the constructor
}