When writing a program for handwriting recognition:
ArrayList<TrainingSet> trainingSets = new ArrayList<>(); for (int i = 0; i < 26; i++) { char letterValue = (char) (i + 65); String letter = String.valueOf(letterValue); for (ArrayList<Integer> list : readFromFile("/resources/" + letter + ".txt")) { trainingSets.add(new TrainingSet(list, GoodOutputs.getInstance().getGoodOutput(letter))); } } return trainingSets; }
Reading in the file is successful. But when I want to write new information to the file:
try { File file = new File("/resources/" + filename + ".txt"); PrintWriter pw = new PrintWriter(new FileOutputStream(file, true)); for (Integer i : input) { pw.write(Integer.toString(i)); } pw.write("\n"); pw.close(); } catch (Exception e) { e.printStackTrace(); } }
A lot of errors come up:
java.io.FileNotFoundException: /resources/O.txt (No such file or directory) at java.base/java.io.FileOutputStream.open0(Native Method) at java.base/java.io.FileOutputStream.open(FileOutputStream.java:299) at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:238) at data.ReadWriteFile.saveToFile(ReadWriteFile.java:54) at gui.MainGui.lambda$setOnClicks$1(MainGui.java:155) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6589) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6354) at java.desktop/java.awt.Container.processEvent(Container.java:2261) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2319) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4914) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4543) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4484) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2305) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
ReadFromFile method:
private static ArrayList<ArrayList<Integer>> readFromFile(String filename) { ArrayList<ArrayList<Integer>> inputs = new ArrayList<>(); try { InputStream in = ReadWriteFile.class.getResourceAsStream(filename); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { ArrayList<Integer> input = new ArrayList<>(); for (int i = 0; i < line.length(); i++) { int value = 0; try { value = Integer.parseInt(String.valueOf(line.charAt(i))); } catch (Exception e) { } input.add(value); } inputs.add(input); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return inputs; }