java-io

java 6 IO - wrapped streams closing [closed]

爷,独闯天下 提交于 2019-12-02 14:23:53
Consider : public static void read(String filename) throws IOException { String charsetName = "UTF-8"; InputStream file = new FileInputStream(filename); // say no problem InputStreamReader reader = new InputStreamReader(file, charsetName); BufferedReader buffer = new BufferedReader(reader); try { buffer.readLine(); } finally { try { buffer.close(); } catch (IOException e) { // report at least e.printStackTrace(); } } } If new InputStreamReader(file, charsetName) throws UnsupportedEncodingException , the buffer.close(); line will never be called. The alternative is extra verbose : InputStream

Java - Skip first line while using try with resources

折月煮酒 提交于 2019-12-02 14:08:58
I need to skip the very first line of what is found in the file. My code: List<String> readStuff() { String pathName = "D:/java/eclipse/someStuff.txt"; List<String> list = new ArrayList<>(); try (Stream<String> lines = Files.lines(Paths.get(pathName))) { list = lines.collect(Collectors.toList()); } catch (IOException e) { System.out.println("Failed to load file."); } return list; } You can just invoke skip(n) to skip the first n-th element from the stream. In this case, using skip(1) would skip the first line. try (Stream<String> lines = Files.lines(Paths.get(pathName))) { return lines.skip(1)

Blocking in terms of java.io/java.nio

梦想的初衷 提交于 2019-12-02 13:12:50
I just read ... Classes that work with streams are located in two packages: java.io and java.nio. Classes from the former implement blocking of input/output (I/O): When bytes are being read/written by a process, they become unavailable for other threads of execution. The latter package offers non-blocking I/O with improved performance. ... and would like to understand this a bit more. Does blocking only impact the single relevant thread, but leave the source (i.e. file or database) itself unblocked, ready to be accessed by other streams? Or does the blocking actually prevent the source from

What is the difference between getResourceAsStream(“Words.txt”) and FileInputStream(“./src/package/Words.txt”)? [duplicate]

半世苍凉 提交于 2019-12-02 13:02:40
This question already has an answer here: getResourceAsStream() vs FileInputStream 6 answers I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1 , SO2 ). But I have tried FileInputStream("./src/package/Words.txt") and the text file can still be successfully loaded. What are the differences? And why is the method

Reading single InputStream from multiple methods

Deadly 提交于 2019-12-02 10:08:22
I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing. Another method calls 2 different methods passing in same InputStream one for retrieving headers and another for processing contents. The structure looks something as given below: main() { FileInputStream fis = new FileInputStream("FileName.CSV"); BufferedInputStream bis = new BufferedInputStream(fis); InputStreamReader isr = new InputStreamReader(bis); processCSV(isr); } processCSV(Reader isr) { fetchHeaders(isr);

How to copy image in java using bufferedreader/writer

…衆ロ難τιáo~ 提交于 2019-12-02 09:55:19
File file = new File("download.png"); File newfile = new File("D:\\Java.png"); BufferedReader br=null; BufferedWriter bw=null; try { FileReader fr = new FileReader(file); FileWriter fw = new FileWriter(newfile); br = new BufferedReader(fr); bw = new BufferedWriter(fw); char[] buf = new char[1024]; int bytesRead; while ((bytesRead = br.read(buf)) > 0) { bw.write(buf, 0, bytesRead); } bw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } Whats wrong with this code. Is it possible with BufferedReader

Updating a bundled resource file

旧城冷巷雨未停 提交于 2019-12-02 07:50:25
I am doing the following, String str = "this is the new string"; URL resourceUrl = getClass().getResource("path_to_resource"); File file = new File(resourceUrl.toURI()); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(xml); writer.close(); In the above code I am trying to write to a resource file contained in one of my java packages. After executing the code, my program executes fine but the file just updates the properties file in web-INF and not into the package where it is stored. Can anyone please help me figure how can I achieve that or what am I doing wrong

Cannot delete folder using Java

℡╲_俬逩灬. 提交于 2019-12-02 07:13:36
问题 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

Infinite loop on Scanner.hasNext, reading from a file

自作多情 提交于 2019-12-02 00:42:21
I'm apparently facing an infinite loop on while(input.hasNext()) , as in following code File file = new File("data.txt"); Scanner input = new Scanner(file); int sum = 0; while (input.hasNext()) { if (input.hasNextInt()) { sum += input.nextInt(); } } System.out.println(sum); Some related questions explain why it always returns true if input stream is System.in , however I'm scanning through a File . Please let me know where I'm going wrong. I'm attempting to calculate the sum of unique integer values (space delimited where they occur). Edit: Lesson learned, input.hasNext() does not move the

Sonar: How to use try-with-resources to close FileOutputStream

假装没事ソ 提交于 2019-12-02 00:01:20
Sonar is giving an error that this FileOutputStream should be closed. I need to modify the following code to use try-with-resources . How do I do this? public void archivingTheFile(String zipFile){ byte[] buffer = new byte[1024]; try{ FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); for(String file : this.fileList){ ZipEntry ze= new ZipEntry(file); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); } zos