java-io

Inserting data in RandomAccessFile and updating index

五迷三道 提交于 2019-12-12 02:03:18
问题 I've got a RandomAccessFile in Java where i manage some data. Simplified: At the start of the file i have an index. (8 byte long value per dataset which represents the offset where the real data can be found). So if i want to now where i can find the data of dataset no 3 for example. I read 8 Bytes at offset (2*8). (Indexing starts with 0). A dataset itsself consists of 4 Bytes which represents the size of the dataset and then all the bytes belonging to the dataset. So that works fine in case

How to read a live file - that is being writen by another writer thread

依然范特西╮ 提交于 2019-12-12 01:43:16
问题 I want to read a (log file being written continuously by an app) Currently I have temp solution using RandomAccessFile (read mode) Is there any other solution to this problem ? 回答1: Java 7 has new API that allows listening to the file system events: http://java.dzone.com/news/how-watch-file-system-changes If you are stuck with previous version of java use poling as described here: File changed listener in Java 回答2: You probably could use FileInputStream to read this file. The fact that

'Stream closed' exception occurs

这一生的挚爱 提交于 2019-12-11 15:48:42
问题 Recently i am developing a code to export data to excel. I used apache POI for developing this functionality. Please consider the below code. public static void prepareDateForCSVGeneration(List<DataList> dataList,HttpServletResponse response) throws CashBookingInquiryServiceApplicationException { try (ServletOutputStream out = response.getOutputStream()) { XSSFWorkbook workbook = new XSSFWorkbook(); //below method creates headers and rows convertToExcel(workbook, dataList); response

Why should we use OutputStream.write(byte[] b, int off, int len) instead of OutputStream.write(byte[] b)?

ⅰ亾dé卋堺 提交于 2019-12-11 15:08:25
问题 Sorry, everybody. It's a Java beginner question, but I think it will be helpful for a lot of java learners. FileInputStream fis = new FileInputStream(file); OutputStream os = socket.getOutputStream(); byte[] buffer = new byte[1024]; int len; while((len=fis.read(buffer)) != -1){ os.write(buffer, 0, len); } The code above is part of FileSenderClient class which is for sending files from client to a server using java.io and java.net.Socket. My question is that: in the above code, why should we

FileInputStream not finding the file [closed]

早过忘川 提交于 2019-12-11 15:03:46
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I am making a program in java that will read a file and put each word from it into an array so I can make an anagram of each word after sorting them to a default array. I have a good idea of how to do this,

How to list latest files in a directory using FileNameFilter

。_饼干妹妹 提交于 2019-12-11 12:45:29
问题 I have a directory which contains xml Files : 20140110-093721565_TOTO.xml 20140110-093821565_TATA.xml 20140110-094021565_TOTO.xml 20140110-091021565_TOTO.xml 20140110-093921565_TATA.xml .... 20140110-091021565_TETE.xml The first part of the file name represents the time the file was generated : 20140110-09102156 20140110 = Date 09102156 = Hour and the last part is used to identify the file : TOTO What I want is to list the last version of each files like : 20140110-091021565_TOTO.xml 20140110

Search directory for any .XML file

删除回忆录丶 提交于 2019-12-11 11:24:37
问题 I am trying to get a piece of code working. The aim is to check if there is an .XML file in a certain directory. This is what I've got so far. File f = new File("saves/*.xml"); if(f.exists()) { /* Do Something */ } else { /* do something else */ } I'm trying to use a wildcard to search for any file ending in .XML, am I missing something simple here? Is there an easier way to check that at least one .XML file exists in a specified directory? thanks in advance. 回答1: You can use this: File dir =

Measure intermediate throughput when writing to OutputStream [or] Why is write() blocking, while read() is not?

萝らか妹 提交于 2019-12-11 06:57:48
问题 I need to measure the intermediate throughput of uploading and downloading a large file (via FTP using Apache Commons Net API). Download: (No problems) Everything is fine for downloading, which uses code like the following: InputStream is = ftp.retrieveFileStream(filePath); byte[] buffer = new byte[256 * 1024]; int read; while ((read = is.read(buffer, 0, buffer.length)) != -1) { Log.v(TAG, "Read = " + read); } Here, the buffer is 256 KB in size, and the total size of the file being downloaded

Effective way to read file and parse each line

爱⌒轻易说出口 提交于 2019-12-11 06:55:12
问题 I have a text file of next format: each line starts with a string which is followed by sequence of numbers. Each line has unknown length (unknown amount of numbers, amount from 0 to 1000). string_1 3 90 12 0 3 string_2 49 0 12 94 13 8 38 1 95 3 ....... string_n 9 43 Afterwards I must handle each line with handleLine method which accept two arguments: string name and numbers set ( see code below ). How to read the file and handle each line with handleLine efficiently? My workaround: Read file

Reading file in Java (IDE used)

≡放荡痞女 提交于 2019-12-11 04:39:13
问题 I was trying to write a piece of Java code to read each line from a file and add it to an Arraylist. Here is my code: // try catch block removed for clarity. file = new TextFileIn("names.txt"); String line = null; while ((line = file.readLine()) != null) { list.add(line); } names.txt was located in the same package folder as my code. The IDE I used was Eclipse. When I run the code however I got a FileNotFoundException . The TextFileIn class comes from http://www.javaranch.com/doc/com