【问题标题】:How to set the file directory so that it will work without the full file path?如何设置文件目录以使其在没有完整文件路径的情况下工作?
【发布时间】:2014-06-08 12:47:05
【问题描述】:

对不起,如果标题令人困惑。问题是我基本上需要将我的 java 程序放在 CD 上的模块上,我想知道如何设置目录以便它可以在没有“C://Users/Haf/Desktop/test.txt”的情况下工作。文本文件”。以下是使用的两个类(不确定您是否都需要):

package javaapplication2;

import java.io.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Haf
 */
public class FileClass {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws IOException {

    // TODO code application logic here

    String file_name = "C://Users/Haf/Desktop/test.txt";

    try {
        ReadFile file = new ReadFile(file_name);
        String [] aryLines = file.OpenFile();

        int i;
        for (i=0; i < aryLines.length; i++) {
            System.out.println(aryLines[i]);
        }
    }        
    catch (IOException e)  {
        System.out.println(e.getMessage () );
    }            
  }    
}

package javaapplication2;

import java.io.*;

public class ReadFile {     //creating a constructor 

  private String path; 

  public ReadFile(String file_path) {
        path = file_path;
  }

  public String[] OpenFile() throws IOException { //returning a string array. You need to use IOException

    FileReader fr = new FileReader (path); //creating FileReader obj called fr
    BufferedReader textReader = new BufferedReader(fr); //bufferedreader obj


    int numberOfLines = readLines();
    String [] textData = new String[numberOfLines]; //array length set by numberOfLines

    int i;

    for (i = 0; i < numberOfLines; i++) {
        textData[i] = textReader.readLine();
    }

    textReader.close();
    return textData;
  }

  int readLines() throws IOException {

    FileReader file_to_read = new FileReader(path); 
    BufferedReader bf = new BufferedReader (file_to_read);

    String aLine; 
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) !=null) {
        numberOfLines++;
    }
    bf.close();
    return numberOfLines;
  }
}

【问题讨论】:

  • 我可以改写您的问题,这可能会让您也有不同的想法并自己解决:如何使应用程序用于加载文件的路径不被硬编码?或者这里是另一个版本:我怎样才能找到用户的主目录?或者另一个版本:我如何存储和加载文件作为我的应用程序的一部分?我不知道你想要真正实现什么,所以我不知道我必须回答哪个替代问题。
  • 我想第三个是最准确的。我基本上希望它类似于 HTML 的工作方式,只要它们在同一个文件夹中,程序就会加载文本文件。如果那不可能,我想为用户找到目录会更好。感谢您的帮助
  • 效率方面,您最好将每一行加载到动态结构中,例如java.util.List,然后将其转换为String[] 作为最后一步。这将避免您必须解析文件两次 - 一次获取行数,再次读取每一行

标签: java file io directory


【解决方案1】:

你可以做两件事。

1) 如果文件需要由用户提供,则将位置作为参数。如果您希望应用程序在没有用户输入的情况下失败,也可以使用这种方法。

public static void main(String... args) {
   String location = args[0];
   ... // Do Stuff
}

2) 如果文件是静态的,则将其打包到您的 jar 中并作为资源读取。

public static void main(String... args) {
   InputStream input = FileClass.getClass().getClassLoader().getResourceAsStream("test.txt")
   ... // Do Stuff
}

当它是一个属性文件时,我的偏好是两者的结合。定义您的默认属性并将它们打包。然后,允许用户通过提供自己的属性来覆盖这些值。从类路径加载属性(示例二),然后替换用户提供的文件中的任何值(示例一)。

【讨论】:

  • 非常感谢您的帮助!
猜你喜欢
  • 2012-07-04
  • 2021-09-08
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多