Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inputting local filepath

Reply
Thread Tools

inputting local filepath

 
 
Justin
Guest
Posts: n/a
 
      12-22-2006
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\\consultr equest\\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();
}
}

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      12-22-2006
On 21 Dec 2006 23:42:01 -0800, Justin wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Phi
Guest
Posts: n/a
 
      12-22-2006
Justin wrote:
> 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\\consultr equest\\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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to request full filepath to a default page? darrel ASP .Net 0 06-24-2005 04:07 PM
Reposting question - How to pull filepath from SQL to use in slide =?Utf-8?B?UnVkeQ==?= ASP .Net 0 05-11-2005 03:48 PM
Filepath lookup from id Andreas Zita ASP .Net 2 02-28-2005 01:38 PM
Previous Level FilePath Larry R Harrison Jr HTML 6 08-19-2004 07:29 PM
current filepath? Lauchlan M ASP .Net 1 08-18-2003 12:04 AM



Advertisments