file input problems (with windows?)

Reply

Join Date: Apr 2007
Posts: 2
Reputation: Zatnik is an unknown quantity at this point 
Solved Threads: 0
Zatnik Zatnik is offline Offline
Newbie Poster

file input problems (with windows?)

 
0
  #1
Apr 2nd, 2007
I've finished my assignment (messily) and it works well enough in the uni linux computers - but I can't make file input work at home under XP. I wouldn't have thought that the windows/linux change would make a difference with the JRE...

(But then I'm new to this "com-puuuu-tah" stuff)

the directory contains:
ClassID.class & .java
ClassRecord.class & .java
RunRecord.class & .java
rec

rec is the file I'm trying to load.

IOException returned is:
(The filename, directory name, or volume label syntax is incorrect)

The section of code that deals with file io is here:

  1. public ClassRecord(String filename)
  2. //load from file
  3. {
  4. FileInputStream fis;
  5. InputStreamReader isr;
  6. ...
  7. try {
  8. fis = new FileInputStream(filename);
  9. isr = new InputStreamReader(fis);
  10. ...
  11. }
  12. }
  13. catch( IOException e)
  14. {
  15. System.out.println("LOADING ERROR! " + filename + "\n" + e);
  16. }
  17. }

The complete code is below:
(If anybody needs to know what the task actually was I can put that in, but I don't think it's nessisary. Nor really is the complete code... but just in case.)

ClassID
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class ClassID
  5. {
  6. /*feilds*/
  7. private String strName;
  8. private int intID;
  9. private int intACN;
  10. private double dblTTim; //military H.MM
  11. private int intTMod;
  12. /*defaults*/
  13. public String dftName = new String("[UNNAMED!]");
  14. public int dftID = 0;
  15. public int dftACN = 0;
  16. public double dftTTim = 0.00;
  17. public int dftTMod = 0;
  18.  
  19. /*CONSTRUCTORS*/
  20. public ClassID()
  21. {
  22. strName = new String(dftName);
  23. intID = dftID;
  24. intACN = dftACN;
  25. dblTTim = dftTTim;
  26. intTMod = dftTMod;
  27. }
  28. public ClassID(String inName, int inID, int inACN, double inTTim, int
  29. inTMod)
  30. //Alternate
  31. {
  32. strName = new String(inName);
  33. intID = inID;
  34. intACN = inACN;
  35. dblTTim = inTTim;
  36. intTMod = inTMod;
  37. }
  38. public ClassID(String inString)
  39. //Alternate2 - 1
  40. {
  41. StringTokenizer sttTokStr;
  42. sttTokStr = new StringTokenizer(inString);
  43. if (sttTokStr.hasMoreTokens())
  44. strName = new String(sttTokStr.nextToken());
  45. else strName = new String(dftName);
  46. if (sttTokStr.hasMoreTokens())
  47. intID = Integer.parseInt( sttTokStr.nextToken());
  48. else intID = dftID;
  49. if (sttTokStr.hasMoreTokens())
  50. intACN = Integer.parseInt( sttTokStr.nextToken());
  51. else intACN = dftACN;
  52. if (sttTokStr.hasMoreTokens())
  53. dblTTim = Double.parseDouble( sttTokStr.nextToken());
  54. else dblTTim = dftTTim;
  55. if (sttTokStr.hasMoreTokens())
  56. intTMod = Integer.parseInt( sttTokStr.nextToken());
  57. else intTMod = dftTMod;
  58.  
  59. System.out.println(toString());
  60. }
  61. public ClassID(ClassID inID)
  62. //Alternate2 - 2 COPY
  63. {
  64. strName = new String(inID.getName());
  65. intID = inID.getID();
  66. intACN = inID.getACN();
  67. dblTTim = inID.getTTim();
  68. intTMod = inID.getTMod();
  69. }
  70. /*MUTATORS*/
  71. //currently no validation.
  72. public void setName(String inName)
  73. {
  74. strName = inName;
  75. }
  76. public void setID(int inID)
  77. {
  78. intID = inID;
  79. }
  80. public void setACN(int inACN)
  81. {
  82. intACN = inACN;
  83. }
  84. public void setTTim(double inTTim)
  85. {
  86. dblTTim = inTTim;
  87. }
  88. public void setTMod(int inTMod)
  89. {
  90. intTMod = inTMod;
  91. }
  92.  
  93. /*ACCESSORS*/
  94. public String getName()
  95. {
  96. return new String(strName);
  97. }
  98. public int getID()
  99. {
  100. return intID;
  101. }
  102. public int getACN()
  103. {
  104. return intACN;
  105. }
  106. public double getTTim()
  107. {
  108. return dblTTim;
  109. }
  110. public int getTMod()
  111. {
  112. return intTMod;
  113. }
  114. public String toString()
  115. /*Format: (space seperated.)
  116. strName intID intACN dblTTim intTMod*/
  117. {
  118. String strTMod = new String("No mode specified");
  119. if( intTMod == 0)
  120. strTMod = "ENTRY";
  121. else if( intTMod == 1)
  122. strTMod = "EXIT";
  123. return new String(strName + " " + intID + " " + intACN + " " +dblTTim + " "
  124. + strTMod);
  125. }
  126. public String saveString()
  127. {
  128. return new String(strName + " " + intID + " " + intACN + " " +dblTTim + " "
  129. + intTMod + ":");
  130. }
  131. public boolean equals(ClassID id2)
  132. //compares id number and name.
  133. //case sensitive
  134. {
  135. boolean sfgrdgkj = false;
  136. if( strName.equals(id2.getName()) && intID == id2.getID())
  137. sfgrdgkj = true;
  138. return sfgrdgkj;
  139. }
  140. }

ClassRecord
  1. import java.io.*;
  2. import java.util.*;
  3. public class ClassRecord
  4. /*
  5. +constructors...
  6. ...
  7. +toString
  8. +save
  9. +add
  10. +getLength
  11. +getCidValue
  12. +findByID
  13. +findByName
  14. */
  15. {
  16. private ClassID cid[];
  17.  
  18. public ClassRecord(String filename)
  19. //load from file
  20. {
  21. FileInputStream fis;
  22. InputStreamReader isr;
  23. int data;
  24. String str = new String();
  25. try {
  26. fis = new FileInputStream(filename);
  27. isr = new InputStreamReader(fis);
  28. data = isr.read();
  29. double dblN = 0.0;
  30. double d = 0.1;
  31. int n = 0;
  32. while( data != (int)':')
  33. {
  34. dblN += d*(double)(data - 48); //48 is 0 code.
  35. data = isr.read();
  36. d = d/10.0;
  37. }
  38. n = (int)(dblN/(d*10.0));
  39. System.out.println("number of entries= " + n);
  40. cid = new ClassID [n];
  41. data = isr.read(); //skip the :
  42. data = isr.read();
  43. int i = 0;
  44. while(i<n)
  45. {
  46. while( data != -1 && data != (int)':')
  47. {
  48. str += (char)data;
  49. data = isr.read();
  50. }
  51. cid[i] = new ClassID(str);
  52. data = isr.read(); //skip :
  53. data = isr.read();
  54. str = "";
  55. i++;
  56. }
  57. }
  58. catch( IOException e)
  59. {
  60. System.out.println("LOADING ERROR! " + filename + "\n" + e);
  61. }
  62. }
  63. public ClassRecord(ClassRecord inRec)
  64. {
  65. cid = new ClassID[inRec.getLength()];
  66. for( int i=0; i<inRec.getLength(); i++)
  67. cid[i] = new ClassID(inRec.getCidValue(i));
  68. }
  69. public ClassRecord(int n)
  70. //constructs an array of size n default elements
  71. {
  72. cid = new ClassID[n];
  73. for( int i=0; i<n; i++)
  74. cid[i] = new ClassID();
  75. }
  76. public void save(String filename)
  77. //write to file
  78. {
  79. FileOutputStream fos;
  80. PrintWriter pw;
  81. try {
  82. System.out.println("SAVING... " + filename);
  83. fos = new FileOutputStream(filename);
  84. pw = new PrintWriter(fos, true);
  85. pw.println(cid.length + ":");
  86. for( int i = 0; i< cid.length; i++)
  87. pw.println(cid[i].saveString());
  88. pw.close();
  89. System.out.println("DONE SAVING!");
  90. }
  91. catch (IOException e)
  92. {
  93. System.out.println("WRITE ERROR! " + filename);
  94. }
  95. }
  96. public void add(ClassID inID)
  97. //add one elment on end
  98. {
  99. int m = cid.length;
  100. int n = m+1;
  101. ClassID cid2[];
  102. cid2 = new ClassID[m];
  103. for( int i=0; i<m;i++)
  104. cid2[i] = new ClassID(cid[i]);
  105. cid = new ClassID [n];
  106. for( int i=0; i<m; i++)
  107. {
  108. cid[i] = new ClassID(cid2[i]);
  109. }
  110. cid[m] = new ClassID(inID);
  111. }
  112. public void findByName(String search)
  113. {
  114. System.out.println("SEARCHING for: " + search +
  115. "\n#.| Name | ID | ACN | time(military) | mode");
  116. int resultCount = 0;
  117. for( int i=0; i< cid.length; i++)
  118. {
  119. if ((cid[i].getName()).equals(search))
  120. {
  121. resultCount++;
  122. System.out.println(i + ". " + cid[i].toString());
  123. }
  124. }
  125. System.out.println("The Search is COMPLETE.\n" +
  126. resultCount + " result(s) found matching your request.");
  127. }
  128. public void findByID(int search)
  129. {
  130. System.out.println("SEARCHING for: " + search);
  131. int resultCount = 0;
  132. for( int i=0; i< cid.length; i++)
  133. {
  134. if (cid[i].getID() == search)
  135. {
  136. resultCount++;
  137. System.out.println(i + ". " + cid[i].toString());
  138. }
  139. }
  140. System.out.println("The Search is COMPLETE.\n" +
  141. resultCount + " result(s) found matching your request.");
  142. }
  143. public int getLength()
  144. {
  145. return cid.length;
  146. }
  147. public ClassID getCidValue(int i)
  148. {
  149. return new ClassID(cid[i]);
  150. }
  151. public String toString()
  152. //for displaying results only
  153. {
  154. String outString = new String("");
  155. int n = cid.length;
  156. for( int i=0; i<n; i++)
  157. outString += (i+1) + ". " + cid[i].toString() + "\n";
  158. return outString;
  159. }
  160. public int getNumPpl()
  161. //finds number of different people in the current array
  162. //uses ClassID equals()
  163. {
  164. /*
  165. ppl = 1
  166. FOR c = 1 to n-1
  167. if beenBefore(c) returns false
  168. then increment ppl
  169. END FOR
  170. */
  171. int n = cid.length;
  172. int i = 0;
  173. int ppl = 1; //num people so far, including 0th element.
  174. for( int c=1; c<n; c++)
  175. {
  176. if(beenBefore(c) == false)
  177. ppl++; //ie, if there have been none the same so far,
  178. //then increment ppl.
  179. }
  180. return ppl;
  181. }
  182. public boolean beenBefore(int c)
  183. //has this person been in a previous ClassID?
  184. {
  185. boolean equ = false;
  186. int n = cid.length;
  187. int i = 0;
  188. i = 0;
  189. while((i<c) && (equ == false))
  190. {
  191. if( cid[i].equals(cid[c]))
  192. equ = true;
  193. i++;
  194. }
  195. return equ;
  196. }
  197. public void groupID()
  198. {
  199. save("rtemp");
  200. ClassRecord rtemp = new ClassRecord("rtemp");
  201. int ct = 0;
  202. int it = 0;
  203. int cc = 0;
  204. int ic = 0;
  205. while(ct<cid.length)
  206. {
  207. if(rtemp.beenBefore(ct) == false)
  208. {
  209. cid[cc] = new ClassID(rtemp.getCidValue(ct)); cc++;
  210. for(it=ct+1;it<cid.length;it++)
  211. {
  212. if((rtemp.getCidValue(it)).equals(cid[(cc-1)]))
  213. {
  214. cid[cc] = new ClassID(rtemp.getCidValue(it)); cc++;
  215. }
  216. }
  217. }
  218. ct++;
  219. }
  220.  
  221. /*
  222. WHILE ct<n
  223. IF t(ct) beenbefore = false
  224. THEN
  225. c(cc) = t(ct)
  226. cc++
  227. FOR (ct<it<n)
  228. IF t(it) = c(ct-1)
  229. THEN
  230. c(cc) = t(it)
  231. cc++
  232. END IF
  233. END FOR
  234. END IF
  235. ct++
  236. END WHILE
  237.  
  238. Run Example:
  239. FROM:
  240. Alexander 1 1 6.0 ENTRY
  241. Birchcoff 2 1 7.0 ENTRY
  242. Cronowijk 3 2 7.01 ENTRY
  243. Dawson 4 3 7.23 ENTRY
  244. Eden 5 2 8.3 ENTRY
  245. Cronowijk 3 2 8.31 EXIT
  246. Alexander 1 1 9.0 EXIT
  247. Birchcoff 2 1 9.1 EXIT
  248. Dawson 4 3 9.11 EXIT
  249. Eden 5 2 9.12 EXIT
  250.  
  251.  
  252. TO:
  253. 1. Alexander 1 1 6.0 ENTRY
  254. 2. Alexander 1 1 9.0 EXIT
  255. 3. Birchcoff 2 1 7.0 ENTRY
  256. 4. Birchcoff 2 1 9.1 EXIT
  257. 5. Cronowijk 3 2 7.01 ENTRY
  258. 6. Cronowijk 3 2 8.31 EXIT
  259. 7. Dawson 4 3 7.23 ENTRY
  260. 8. Dawson 4 3 9.11 EXIT
  261. 9. Eden 5 2 8.3 ENTRY
  262. 10. Eden 5 2 9.12 EXIT
  263.  
  264.  
  265. */
  266. }
  267. public void viewDurations()
  268. {
  269. int imin, imax;
  270. groupID();
  271. int c = 0;
  272. int k = 0;
  273. String strDur[];
  274. strDur = new String[getNumPpl()];
  275. while( c<cid.length)
  276. {
  277. imin = c;
  278. //while the next element has the same person as this one
  279. if( c+1<cid.length)
  280. if( cid[c+1].equals(cid[c]))
  281. c++;
  282. imax = c;
  283. strDur[k] = new String(calcTotalTime(imin, imax) + ": " +
  284. cid[c].getID() + " " + cid[c].getName());
  285. k++; c++;
  286. }
  287. //SORT BY THE durations - not coded
  288. System.out.println("_______________________________" +
  289. "\nduration | ID | Name\n"+
  290. "[-ve duration: worker did not leave]");
  291. for(k=0;k<getNumPpl();k++)
  292. System.out.println(strDur[k]);
  293. /*
  294. sort, if things are same, calcTotalTime(imin, imax),
  295. stick stuff in an array of strings or make some other object,
  296. then sort them into the correct order.
  297. */
  298. }
  299. private double calcTotalTime(int imin, int imax)
  300. {
  301. /*
  302. IMPORTS: -
  303. EXPORTS: total (a double)
  304. double total = 0.0
  305. FOR (imin <= i <= imax)
  306. IF cid[i].getTMod() = 0
  307. THEN total = total - cid[i].getTTim()
  308. ELSE total += cid[i].getTTim()
  309. END FOR
  310. */
  311. int hours = 0;
  312. int mins = 0;
  313. int t = 0;
  314. for( int i = imin; i<=imax; i++)
  315. {
  316. t = (int)(100.00*cid[i].getTTim());
  317. System.out.println(cid[i].getTTim());
  318. if(cid[i].getTMod() == 0)
  319. {
  320. hours = hours - t/100;
  321. mins = mins - (t%100);
  322. }
  323. else
  324. {
  325. hours += t/100;
  326. mins += t%100;
  327. }
  328. }
  329. hours += mins/60; //note: integer division.
  330. mins = mins - 60*(mins/60); // note: integer division.
  331. if( mins<0)
  332. {
  333. mins = mins+60;
  334. hours = hours -1;
  335. }
  336. return ((double)hours + ((double)(mins))/100.0);
  337. /*
  338. ASSERTION - data is valid.
  339. Valid data:
  340. is in pairs of 01! starts on 0, ends with 1,
  341. 0 never follows 0 and 1 never follows 0,
  342. and only 0 and 1 are present.
  343.  
  344. Run Example:
  345. from:
  346. Name/ID/ACN/Time(H.MM)/mode
  347. 1. Alexander 1 1 6.0 ENTRY
  348. 2. Alexander 1 1 9.0 EXIT
  349. 3. Birchcoff 2 1 7.0 ENTRY
  350. 4. Birchcoff 2 1 9.1 EXIT
  351. 5. Cronowijk 3 2 7.01 ENTRY
  352. 6. Cronowijk 3 2 8.31 EXIT
  353. 7. Dawson 4 3 7.23 ENTRY
  354. 8. Dawson 4 3 9.11 EXIT
  355. 9. Eden 5 2 8.3 ENTRY
  356. 10. Eden 5 2 9.12 EXIT
  357. 11. Henderson 6 3 0.1 ENTRY
  358. 12. Henderson 6 4 1.0 EXIT
  359. 13. Joans 7 5 9.0 ENTRY
  360. 14. Mapel 8 3 9.0 ENTRY
  361. 15. Mapel 8 3 9.01 EXIT
  362. 16. Jhonston 9 2 6.0 ENTRY
  363. to:
  364. duration | ID | Name
  365. [-ve duration: worker did not leave]
  366. 3.0: 1 Alexander
  367. 2.1: 2 Birchcoff
  368. 1.3: 3 Cronowijk
  369. 1.48: 4 Dawson
  370. 0.41: 5 Eden
  371. 0.5: 6 Henderson
  372. -9.0: 7 Joans
  373. 0.01: 8 Mapel
  374. -6.0: 9 Jhonston
  375. */
  376. }
  377. }

RunRecord
  1. import io.*;
  2. import java.io.*;
  3.  
  4. public class RunRecord
  5. {
  6. public static void main( String []args)
  7. {
  8. String filename = new String(ConsoleInput.readLine(
  9. "Please Enter filename to load/prep\n" +
  10. "[a non-existant file will go to main menu]"));
  11. ClassRecord rec = new ClassRecord(filename);
  12.  
  13. boolean timeToBuggerOff = false;
  14. do {
  15. System.out.println("_______________________________" +
  16. "\nSECURITY RECORD - MAIN MENU\n" +
  17. "Apparent file: " + filename + "\n" +
  18. "1 - LOAD a new file\n" +
  19. "2 - SAVE changes OVER " + filename + "\n" +
  20. "3 - SAVE AS\n" +
  21. "4 - ADD a new entry\n" +
  22. "5 - SEARCH for an entry\n" +
  23. "6 - VEIW current file\n" +
  24. "7 - VEIW sign in durations\n" + "8 - QUIT");
  25. int choice = ConsoleInput.readInt(
  26. "Please enter your selection and press ENTER");
  27. if (choice == 1)
  28. {
  29. filename = ConsoleInput.readLine(
  30. "Please Enter filename");
  31. rec = new ClassRecord(filename);
  32. }
  33. else if (choice == 2)
  34. rec.save(filename);
  35. else if (choice == 3)
  36. {
  37. filename = ConsoleInput.readLine(
  38. "Please Enter filename");
  39. rec.save(filename);
  40. }
  41. else if (choice == 4)
  42. {
  43. System.out.println("NEW ENTRY:");
  44. rec.add(new ClassID(ConsoleInput.readLine("Name") +
  45. " " + ConsoleInput.readLine("ID") +
  46. " " + ConsoleInput.readLine("Access Control Number (ACN)") +
  47. " " + ConsoleInput.readLine("time (in the form HH.MM, otherwise [Deleted For Security Reasons] you do wrong.)") +
  48. " " + ConsoleInput.readLine("Mode:\n0.Entry\n1.Exit\n (enter number only. See above.)")));
  49. }
  50. else if (choice == 5)
  51. {
  52. int choice2 = ConsoleInput.readInt(
  53. "Search by:\n1.Name\n2.ID number\nENTER NUMBER ONLY");
  54. if (choice2 == 1)
  55. rec.findByName(ConsoleInput.readLine("Enter Name"));
  56. else
  57. rec.findByID(ConsoleInput.readInt("Enter ID"));
  58. }
  59. else if (choice == 6)
  60. {
  61. System.out.println("_______________________________\n" +
  62. "Name/ID/ACN/Time(H.MM)/mode\n" + rec.toString());
  63. }
  64. else if (choice == 8)
  65. timeToBuggerOff = true;
  66. else if (choice == 7)
  67. {
  68. rec.viewDurations();
  69. }
  70. else
  71. System.out.println("Invalid entry.");
  72. }
  73. while (timeToBuggerOff == false);
  74. }
  75. }

rec
16:
Alexander 1 1 6.0 0:
Birchcoff 2 1 7.0 0:
Henderson 6 3 0.1 0:
Mapel 8 3 9.01 1:
Jhonston 9 2 6.0 0:
Cronowijk 3 2 8.31 1:
Eden 5 2 9.12 1:
Dawson 4 3 9.11 1:
Eden 5 2 8.3 0:
Alexander 1 1 9.0 1:
Henderson 6 4 1.0 1:
Joans 7 5 9.90 0:
Dawson 4 3 7.23 0:
Birchcoff 2 1 9.1 1:
Cronowijk 3 2 7.01 0:
Mapel 8 3 9.0 0:
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: file input problems (with windows?)

 
0
  #2
Apr 2nd, 2007
Error you get is: "IOException returned is:
(The filename, directory name, or volume label syntax is incorrect)"
Did you check if you're giving the path correctly? May be it's some simple bug like using "/" instead of "\" (/ is for Linux/Unix and \ for windows as you know)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: file input problems (with windows?)

 
0
  #3
Apr 2nd, 2007
Originally Posted by thekashyap View Post
Error you get is: "IOException returned is:
(The filename, directory name, or volume label syntax is incorrect)"
Did you check if you're giving the path correctly? May be it's some simple bug like using "/" instead of "\" (/ is for Linux/Unix and \ for windows as you know)
Just as a note. You can use "/" in Java on both Windows and all Unix flavors (I haven't got a Mac to try, but I'm fairly sure you can use it there, too). So, for convenience sake (and efficiency sake, since it will eliminate errors such as using a single, instead of a double, "\") it is better to either always use "/", or, if you don't like this, to always use File.separator
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: file input problems (with windows?)

 
0
  #4
Apr 3rd, 2007
@masijade:
Interesting..
Checked and it works as well..

@Zatnik
Can you please post the full stack trace that you got.. ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: Zatnik is an unknown quantity at this point 
Solved Threads: 0
Zatnik Zatnik is offline Offline
Newbie Poster

Re: file input problems (with windows?)

 
0
  #5
Apr 3rd, 2007
stack trace? Unfortunatly I'm new to this, so I don't actually know what that is. (and "a printout of the calling stack with the current routine at the top of the trace, showing the name of any violated assertions," doesn't mean much to me. Sorry.)

With the linix computers I didn't need to use a path - just "rec" worked, I'm guessing since "rec" is in the directory I'm running my program from - I find I can load rec if I recompile with the path inside the program itself,
  1. //fis = new FileInputStream(filename);
  2. fis = new FileInputStream("C:\\Documents and Settings\\Step\\My Documents\\Curtin\\2007-Sem1\\ST152\\W01\\rec");
but that rather defies the point of trying to load and save different files by typing them into the command line. I suppose I could always put ("C:\\Documents and Settings\\Step\\My Documents\\Curtin\\2007-Sem1\\ST152\\W01\\" + filename), but then (if I made something similar and wanted to use it on different computers) I'de have to recompile it with whatever path every single tmie.

But possibly I'm working towards my own solution: is there something I can put into the code that will just mean "this current directory here"?
Also, how would I got about typing the path into the command line.

Oh, on testing, ("C:\\Documents and Settings\\Step\\My Documents\\Curtin\\2007-Sem1\\ST152\\W01\\" + filename) doesn't work, though ("C:\\Documents and Settings\\Step\\My Documents\\Curtin\\2007-Sem1\\ST152\\W01\\rec") does. I have no idea why.

(thanks for looknig at this.)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: file input problems (with windows?)

 
0
  #6
Apr 3rd, 2007
I will go out on a limb and say that the problem arises form the following:

  1. filename = ConsoleInput.readLine( "Please Enter filename");
  2. rec.save(filename);

As I am fairly sure that you will be entering the filename as follows:
  1. C:\Bogus\Path\To\File.txt

if this is the case you need to modify the above two lines as follows:
  1. filename = ConsoleInput.readLine( "Please Enter filename");
  2. rec.save(filename.replaceAll("\\", "\\\\"));
  3. // or rec.save(filename.replaceAll("\\", "/"));

As otherwise, you are saving a String with single "\" characters in it, and so, when you attempt to open the file it will interpret the "\" and the character directly after it as special constructs, so you need to escape each of those single "\" characters with a double "\" character, but since "\" is a special character in the replace Strings, you need to escape it there as well, which is the reason for the double and then quadruple "\" characters in the replace statement. The double "\" will find all instances of a "\" and the quadruple "\" will place a double "\" in all those places.

I hope this makes sense to you, as I can't really explain it any better.
Last edited by masijade; Apr 3rd, 2007 at 6:51 am. Reason: I was posting this at the same time the OP was making his last post.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: file input problems (with windows?)

 
0
  #7
Apr 4th, 2007
In other words:
You need to understand how the escape characters/sequences work in Java. When you prefix a character in a string with a '\' you are escaping it. When you escape a character it's meaning changes as masijade explained..
Finally abt the stack trace.. In your code replace:
  1. catch( IOException e)
  2. {
  3. System.out.println("LOADING ERROR! " + filename + "\n" + e);
  4. }

with

  1. catch( IOException e)
  2. {
  3. System.out.println("IOException caught. Stack trace is:");
  4. e.printStackTrace();
  5. }

Now run your program with the wrong path and see what is printed on the screen. That's your stack trace.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC