I am trying to read a file and serialize it through the code below. However, I keep getting the file not found error. I have tried putting the file in the folder, on the desktop and creating a new file in eclipse itself. Please help.
Code:
public class Util implements Serializable {
public static void main (String[] args) throws FileNotFoundException {
try {
Automobile ford = new Automobile();
FileReader fr = new FileReader(
"C:/Users/FName LName/Desktop/Data.txt");
BufferedReader br = new BufferedReader (fr);
String line = new String ();
FileOutputStream fos = new FileOutputStream ("car.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream ("car.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
int k=0;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
String s = new String ();
if (st.hasMoreTokens()) {
s = st.nextToken();
}
OptionSet list = new OptionSet ();
int j = 0;
while (st.hasMoreTokens()) {
String temp = st.nextToken();
String name = new String ();
int price = 0;
StringTokenizer token = new StringTokenizer (temp);
while (token.hasMoreTokens()) {
name = token.nextToken(",");
price = Integer.parseInt(token.nextToken(","));
list.setOption(j,name,price);
}
j++;
}
list.setName(s);
ford.getOs().add(k, list);
k++;
}
oos.writeObject(ford);
ford = (Automobile) ois.readObject();
for(int i=0;i< ford.getOs().size();i++) {
System.out.print(ford.getOs().get(i).getName()+":");
for(int j=0;j<ford.getOs().get(i).getOpt().size();j++) {
System.out.print(ford.getOs().get(i).getOpt().get(j).getName() +
"," + ford.getOs().get(i).getOpt().get(j).getCost()+" ");
}
System.out.println();
}
ois.close();
oos.flush();
oos.close();
br.close();
} catch (IOException e) {
System.out.println("File not found");
} catch (ClassNotFoundException e) {
System.out.println("File not found");
}
}
}
Can you try renaming the folder FName LName
so it has no spaces? Spaces may be an issue. Also try it with backslashes (escaped with an additional \\
): C:\\\\Users\\\\FName LName\\\\Desktop\\\\Data.txt
Also check if you have permissions to that file. Try moving the file to the C:\\
and see if it's readable.
If none of that works, you can try to check if the JVM can see the file:
File f = new File(filePathString);
if(f.exists()) { /* do something */ }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.