inputting local filepath

J

Justin

I am attempting to input a database location from a txt file located
within the build folder. However, I am having a bitchin time getting
it to work with relative file location. So far, I can only get
absolute file names to work, this is useless. Any help would be
appreciated. My code can be found below....



import java.sql.*;
import javax.swing.DefaultListModel;
import java.io.*;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;


public class DataConnection {

public DataConnection(){
DataInputStream input;
String name = "";
File dBLocation = new File("C:\\Documents and
Settings\\Owner.JustinLaptop\\Desktop\\USC
Programs\\ConsultRequest\\build\\classes\\consultrequest\\testing\\dBURL.txt");

dBLocation = new File("..consultrequest\\testing\\dBURL.txt");
System.out.println(dBLocation.getParentFile());

System.out.println(dBLocation.exists());
try { // try to create an object representing the user-selected file
input = new DataInputStream( new FileInputStream(dBLocation) );

try { // try to read the file and create a string consisting of
its contents
char ch;
do { // read data from the file until end of file is reached
name = "";
ch = input.readChar();
while ( ch != ';' ) {
name = name + ch;
ch = input.readChar();
}
}

while ( true );
} catch ( Exception ex ) {
}

try { // try to close the file
input.close();
} catch ( Exception ex ) {
}

} catch ( Exception ex ) {
System.out.println("Database location cannot be found");
}

System.out.println(name);
}


public static void main(String[] args){

DataConnection data = new DataConnection();
}
}
 
G

Gordon Beaton

I am attempting to input a database location from a txt file located
within the build folder. However, I am having a bitchin time getting
it to work with relative file location.
[...]

dBLocation = new File("..consultrequest\\testing\\dBURL.txt");

Probably you meant "..\\consultrequest" here.

Also, realize that to work, the path must be relative to the working
directory at runtime, not compile time.

/gordon
 
P

Phi

Justin said:
I am attempting to input a database location from a txt file located
within the build folder. However, I am having a bitchin time getting
it to work with relative file location. So far, I can only get
absolute file names to work, this is useless. Any help would be
appreciated. My code can be found below....



import java.sql.*;
import javax.swing.DefaultListModel;
import java.io.*;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;


public class DataConnection {

public DataConnection(){
DataInputStream input;
String name = "";
File dBLocation = new File("C:\\Documents and
Settings\\Owner.JustinLaptop\\Desktop\\USC
Programs\\ConsultRequest\\build\\classes\\consultrequest\\testing\\dBURL.txt");

dBLocation = new File("..consultrequest\\testing\\dBURL.txt");
System.out.println(dBLocation.getParentFile());

System.out.println(dBLocation.exists());
try { // try to create an object representing the user-selected file
input = new DataInputStream( new FileInputStream(dBLocation) );

try { // try to read the file and create a string consisting of
its contents
char ch;
do { // read data from the file until end of file is reached
name = "";
ch = input.readChar();
while ( ch != ';' ) {
name = name + ch;
ch = input.readChar();
}
}

while ( true );
} catch ( Exception ex ) {
}

try { // try to close the file
input.close();
} catch ( Exception ex ) {
}

} catch ( Exception ex ) {
System.out.println("Database location cannot be found");
}

System.out.println(name);
}


public static void main(String[] args){

DataConnection data = new DataConnection();
}
}

Hello
Your "..consultrequest\\testing\\dBURL.txt" is a strange notation for a
relative file (it is missing something after the two dots "..").
Try "../consultrequest/testing/dBURL.txt" instead.
Or simpler: use the "parent working direcotry (pwd)" and put your File
"dBURL.txt" to the same directory where you start "java ..." (the
virtual machine. Then you should be able to open your file simply using

String fileName = "dBURL.txt";
FileReader fr = new FileReader(fileName);

phi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top