-
Stack (file location)
Hi,
I have this peace of Java code and I would like to know how to change the file location in the code. (only a filename (test.txt) is in the code and I would like to add a path (c:\test.txt).
Code:
import java.io.*;
import java.util.*;
class StackDemoUsingArrays{
static String [] myString = new String[3];
static int count = 0;
public static void main (String args[]) throws Exception {
BufferedReader br = new BufferedReader
(new InputStreamReader(new FileInputStream(new File("test.txt")))); // <-- I want to change this
String str = "";
while((str = br.readLine()) != null){
push(str.trim()); }
System.out.println(pop()); }
public static String pop(){
if(count<=0) {
System.out.println("Cannot pop an empty stack");
System.exit(0); }
return myString[-- count]; }
public static void push(Object o){
if(count == myString.length){
String [] myString1 = new String[myString.length*2];
System.arraycopy((Object)myString,0,(Object)myString1,0,myString.length);
myString = myString1; }
myString[count] = o.toString();
count++; }
}
-
would you not just change it to c:\test.txt???
One word of warning. that code would only work on Windows Machines as there is no such thing as c: on unix (can't speak for mac, but its based on unix so i assume its the same)
Mrs K
-
Yeah wouldn't you just add in the path? I wouldn't hardcode the file name though. File names are seriously platform dependent.