java-io

When Use FileChannel to read()/write() files?

Deadly 提交于 2019-12-11 02:24:44
问题 I am reading the book Thinking in Java, which explains the java.nio.* package and says that NIO is faster than reading and writing files with traditional IO streams. Why? I have reviewed the following information: IO stream is byte oriented, traditional IO processing unit is byte, and NIO processing unit is block (byte array), but I think traditional IO can also directly process block (byte array) through BufferedFile*, and traditional IO also has direct Method of processing byte array

How to get a random line from text file in Android using Android Studio 2.1.3?

纵然是瞬间 提交于 2019-12-10 23:38:48
问题 I have a text file with 500 lines. I placed this text file in app/src/main/assets folder with the name "words.txt". In this file each line is separated with line break. Now i need to get random line from this text file. I visited following questions prior to posting this. How to load random line from text file in android? InputStreamReader and reading random lines from .txt file How to grab a random line from a text file and print the line [duplicate] How to get a random line of a text file

Problem accessing file from jar file in Java

早过忘川 提交于 2019-12-10 17:44:37
问题 I have a jar called App.jar and it's structure is as follows App.jar | | |---xyzfolder | | | |--config | | | |--config.properties | | |---com (contains classes) | |--MyClass.class Now what I want is that I want to access config.properties file from MyClass.class 回答1: Have you tried the following? this.getClass().getClassLoader().getResourceAsStream("classpath:/xyzfolder/config/config.properties"); see http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html

How can i read the same file two times in Java?

旧时模样 提交于 2019-12-10 14:15:20
问题 I want to counter the lines of the file and in the second pass i want to take every single line and manipulating it. It doesn't have a compilation error but it can't go inside the second while ((line = br.readLine()) != null) . Is there a different way to get the lines(movies) of the file and storing in an array ? BufferedReader br = null; try { // try to read the file br = new BufferedReader(new FileReader("movies.txt")); String line; int numberOfMovies = 0; while ((line = br.readLine()) !=

How to read a TIFF file by tiles with Java?

蓝咒 提交于 2019-12-10 13:54:54
问题 Let's say I have a very large TIFF image as input. I am not able to load this image entirely because of memory specification I must comply with. So the following is not an option : BufferedImage data = ImageIO.read(image); Is there any Java library allowing to read a specific part of the image without buffering the whole image ? Or some ways to get TIFF tiles from a stream ? 回答1: ImageIO can provide you an ImageReader for Tiff, and then you can use readTile. ImageIO has several

FileNotFoundException (no such file or directory)

試著忘記壹切 提交于 2019-12-10 13:52:16
问题 I'm writing an android app and I need to read several files from several folders and add them to several zip archives. I need to limit the max size of the archives to lets say 16mb. So at runtime while adding the files to the archive if the size of it exceeds 16 mb create another archive with the same size limit and so on. I'm using the following wrapper class: import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java

Spring Mvc java.io.FileNotFoundException - ApplicationContext.xml

空扰寡人 提交于 2019-12-10 10:55:09
问题 The applicationContext.xml is in the WEB-INF folder, why am i getting this error : org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist Web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun

Android bluetooth socket.connect fails; Throws java IO exception

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 10:14:48
问题 I'm using a code derived from Bluetooth tutorial on Android Developer page. My app tries to connect to a BT weighing scale and record the weight information. My program throws an error when it attempts to perform a BluetoothSocket.connect(); The program runs as a Bluetooth client/slave in order to receive the data from a BT device. The user sees a list of paired devices on the screen and clicks on a particular device in order to connect to it. Hence, when the user clicks on a specific device,

Does argument/parameters InputStream need to be closed in android?

余生颓废 提交于 2019-12-10 08:37:54
问题 All stream and bufferedReader need to be closed my question is what if the stream and bufferedReader is inside a method arguments/parameters need to be closed also? example normal code: InputStream i = entity.getContent(); i.close(); Q: What if its inside an arguments of a method that has been pass only public void doDownload(InputStream i, BufferedReader b) { i.close(); b.close(); } should i also close it even that inputstream and bufferedreader is only an arguments and don't have an Object?

Can apache FileUtils.writeLines() be made to append to a file if it exists

99封情书 提交于 2019-12-10 04:22:53
问题 The commons FileUtils look pretty cool, and I can't believe that they cannot be made to append to a file. File file = new File(path); FileUtils.writeLines(file, printStats(new DateTime(), headerRequired)); The above just replaces the contents of the file each time, and I would just like to keep tagging this stuff to end just as this code does. fw = new FileWriter(file, true); try{ for(String line : printStats(new DateTime(), headerRequired)){ fw.write(line + "\n"); } } finally{ fw.close(); }