Question

sorry if the title was confusing. The problem is that I basically need to hand in my java program in on a CD for a module and I was wondering how to set up the directory so that it would work without having "C://Users/Haf/Desktop/test.txt". Below are the two classes used (Not sure you'll need both):

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;
  }
}
Was it helpful?

Solution

There are two things you can do.

1) If the file needs to be supplied by the user, take the location as an argument. Also use this approach if you want the application to fail without the user input.

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

2) If the file is static, package it in your jar and read it as a resource.

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

When it's a properties file, my preference is a combination of the two. Define your default properties and package them. Then, allow the user to overwrite the values by supplying their own properties. Load the properties from the classpath (example two), and then replace any values from the file the user supplies (example one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top