java-io

Cannot delete folder using Java

不羁岁月 提交于 2019-12-01 23:22:25
I am trying to delete a folder which has only files but no sub folders without success. Code: File rowFolder = new File(folderPath); String[] files = rowFolder.list(); for (String file : files){ File deleteFile = new File(file); System.out.println("deleting file -"+deleteFile.getName()); deleteFile.delete(); } System.out.println("deleting folder -"+rowFolder.getName()); rowFolder.delete(); Output: deleting file -testing.pdf deleting file -app_json.json deleting file -photo.jpg deleting folder -bundle_folder The code doesn't delete any folder nor any file. Why is that? You could be getting a

Serialization:java.io.StreamCorruptedException: invalid stream header: 0AACED00

隐身守侯 提交于 2019-12-01 17:23:11
I'm a student practicing my File IO skills and I am coming up against a problem with reading Objects from a file using ObjectInputStream. The code is consistently throwing an InvalidClassException and I can't find how the code is throwing it online or by trial and error. Here's my code: import java.io.*; import java.util.ArrayList; import java.util.List; public class ReadFromFile { String filename; List<Object> os; public ReadFromFile(String filename) { this.filename = filename; os = new ArrayList<>(); } public Object[] readObject() { try { FileInputStream fis = new FileInputStream(filename);

Write bytes into a file without erasing existing bytes [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:01:59
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Best Way to Write Bytes in the Middle of a File in Java I have a file in which I need to write bytes. I know at which position in the file I need to insert specific bytes. To make things clear, I need to write bytes in the middle of the file without erasing any existing bytes. The whole operation should then increase the length of the file. What is the best way to do so? 回答1: The only way to do this is to

Java loading binary files

只愿长相守 提交于 2019-12-01 14:46:40
Please show me the best/fast methods for: 1) Loading very small binary files into memory. For example icons; 2) Loading/reading very big binary files of size 512Mb+. Maybe i must use memory-mapped IO? 3) Your common choice when you do not want to think about size/speed but must do only thing: read all bytes into memory? Thank you!!! P.S. Sorry for maybe trivial question. Please do not close it;) P.S.2. Mirror of analog question for C#; For memory mapped files, java has a nio package: Memory Mapped Files Check out byte stream class for small files: Byte Stream Check out buffered I/O for larger

Java- export to jar- i/o problems

倾然丶 夕夏残阳落幕 提交于 2019-12-01 14:04:40
I work on Eclipse (Juno with JDK7), and the program runs (on Eclipse) fine. My problematic lines are: URL imageURL = new URL("http://www.idautomation.com/ocr-a-and-ocr-b-fonts/new_sizes_ocr.png"); RenderedImage img = ImageIO.read(imageURL); File outputfile = new File("saved.png"); ImageIO.write(img, "png", outputfile); But when i export the project to a jar file and try to run it via windows (7- 64 bit) command line, the following error appears: Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect

Java loading binary files

淺唱寂寞╮ 提交于 2019-12-01 13:23:23
问题 Please show me the best/fast methods for: 1) Loading very small binary files into memory. For example icons; 2) Loading/reading very big binary files of size 512Mb+. Maybe i must use memory-mapped IO? 3) Your common choice when you do not want to think about size/speed but must do only thing: read all bytes into memory? Thank you!!! P.S. Sorry for maybe trivial question. Please do not close it;) P.S.2. Mirror of analog question for C#; 回答1: For memory mapped files, java has a nio package:

How to avoid OutOfMemory exception while reading large files in Java

徘徊边缘 提交于 2019-12-01 12:58:51
I am working on the application which reads large amounts of data from a file. Basically, I have a huge file (around 1.5 - 2 gigs) containing different objects (~5 to 10 millions of them per file). I need to read all of them and put them to different maps in the app. The problem is that the app runs out of memory while reading the objects at some point. Only when I set it to use -Xmx4096m - it can handle the file. But if the file will be larger, it won't be able to do that anymore. Here's the code snippet: String sampleFileName = "sample.file"; FileInputStream fileInputStream = null;

get count number of HashMap value

眉间皱痕 提交于 2019-12-01 11:43:27
问题 Using the code from this link loading text file contents to GUI: Map<String, String> sections = new HashMap<>(); Map<String, String> sections2 = new HashMap<>(); String s = "", lastKey=""; try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) { while ((s = br.readLine()) != null) { String k = s.substring(0, 10).trim(); String v = s.substring(10, s.length() - 50).trim(); if (k.equals("")) k = lastKey; if(sections.containsKey(k)) v = sections.get(k) + v; sections.put(k,v);

How to avoid OutOfMemory exception while reading large files in Java

試著忘記壹切 提交于 2019-12-01 10:56:47
问题 I am working on the application which reads large amounts of data from a file. Basically, I have a huge file (around 1.5 - 2 gigs) containing different objects (~5 to 10 millions of them per file). I need to read all of them and put them to different maps in the app. The problem is that the app runs out of memory while reading the objects at some point. Only when I set it to use -Xmx4096m - it can handle the file. But if the file will be larger, it won't be able to do that anymore. Here's the

PrintWriter add text to file

不羁岁月 提交于 2019-12-01 09:25:25
In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a separate method. This is my current method: public static void writeResultsToFile(double g) throws IOException{ PrintWriter outFile = new PrintWriter(new File("planetaryData.txt")); outFile.printf("%.2f%n", g); outFile.close(); } My problem is that when I write it to a file it will overwrite the previous value. How do I get it include all of the values.