inputstream

Using reinterpret_cast to read file into structure

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-20 04:58:25
问题 struct DATAs { char data1; short data2; short data3; float data4; int data5; short data6; unsigned short data7; short data8; char data9; }; void fixFile(char* filename) { std::ifstream InputFile; InputFile.open(filename, std::ios::binary); DATAs FileDatas; InputFile.read(reinterpret_cast<char*>(&FileDatas), sizeof(FileDatas)); } Why do I need to use "reinterpret_cast" for the reading instead of "InputFile.read(&FileDatas, sizeof(FileDatas))" ? 回答1: The type of the first argument to std:

when does FileInputStream.read() block?

不打扰是莪最后的温柔 提交于 2021-02-18 12:10:58
问题 The question is similar to the following two questions. Java InputStream blocking read Why is the FileInputStream read() not blocking? But I still cannot fully understand it. So far I think the read() method in following code will block due to the empty file 'test.txt'. FileInputStream fis = new FileInputStream("c:/test.txt"); System.out.println(fis.read()); System.out.println("to the end"); Actually it will print -1, I want to know why. The javadoc says This method blocks if no input is yet

Java NIO Files count() Method for Counting the Number of Lines

和自甴很熟 提交于 2021-02-10 20:20:51
问题 I was using the below code to calculate the number of lines in a file. long numberOfLines = Files.lines(filePath).count(); Based on the output of the above, if the number of lines is 1, then the record delimiter (extracted from the last character of the line) should be replaced by <delimiter>\n . The delimiter got replaced fine, but when I tried to delete this file after this operation, it did not work. if (numberOfLines == 1) { String rawData = (new String(Files.readAllBytes(filePath))).trim

Java NIO Files count() Method for Counting the Number of Lines

白昼怎懂夜的黑 提交于 2021-02-10 20:19:32
问题 I was using the below code to calculate the number of lines in a file. long numberOfLines = Files.lines(filePath).count(); Based on the output of the above, if the number of lines is 1, then the record delimiter (extracted from the last character of the line) should be replaced by <delimiter>\n . The delimiter got replaced fine, but when I tried to delete this file after this operation, it did not work. if (numberOfLines == 1) { String rawData = (new String(Files.readAllBytes(filePath))).trim

How much data does inputstream.read reads in java

邮差的信 提交于 2021-02-10 14:53:16
问题 I was looking at the definition of the read method of inputstream object, I am confused about how much data is read everytime since it says "reads some bytes" public int read(byte[] b) throws IOException Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. Lets say i have a buffer array of size

Find out whether an InputStream is closed

梦想与她 提交于 2021-02-08 14:08:58
问题 Does Java have any reliable way of determining whether a java.io.InputStream is closed before I attempt to read from it? My use case is that I have a method that takes an InputStream argument and reads from it. The method runs in its own thread, and I want to terminate the thread if the input stream is closed. InputStream implements Closeable , which provides a close() method but apparently no way to query if the instance has already been closed. Attempting to read from closed InputStream

Find out whether an InputStream is closed

时光毁灭记忆、已成空白 提交于 2021-02-08 14:07:33
问题 Does Java have any reliable way of determining whether a java.io.InputStream is closed before I attempt to read from it? My use case is that I have a method that takes an InputStream argument and reads from it. The method runs in its own thread, and I want to terminate the thread if the input stream is closed. InputStream implements Closeable , which provides a close() method but apparently no way to query if the instance has already been closed. Attempting to read from closed InputStream

Is it possible to use try with resources along with an input stream?

て烟熏妆下的殇ゞ 提交于 2021-02-08 11:47:19
问题 When implementing try with resources I am creating a Scanner object via Scanner sc = new Scanner(System.in) within () of the try statement. In the try block, I am prompting the user to enter a numeric value, reading it via sc.nextLine() and utilizing parseDouble to convert it to a method. I utilize a do-while loop to re-prompt the user to enter a value if an invalid value was entered initially. However, if the user enters an invalid value, the input stream closes, NumberFormatException is

Android/Java: How to track progress of InputStream

做~自己de王妃 提交于 2021-02-08 05:28:15
问题 I have the following code in my Android app, and not sure whether it is my Googling skills, but am not able to find a good tutorial on how to monitor progress of InputStream. private void restoreFromUri(Uri uri) { try { InputStream is = getContentResolver().openInputStream(uri); ObjectInputStream ois = new ObjectInputStream(is); ArrayList<String> myList = (ArrayList) ois.readObject(); ois.close(); is.close(); } catch (Exception e) { Snackbar.make(snackView, e.getMessage(), Snackbar.LENGTH

InputStream or Reader wrapper for progress reporting

自作多情 提交于 2021-02-07 05:26:26
问题 So, I'm feeding file data to an API that takes a Reader , and I'd like a way to report progress. It seems like it should be straightforward to write a FilterInputStream implementation that wraps the FileInputStream , keeps track of the number of bytes read vs. the total file size, and fires some event (or, calls some update() method) to report fractional progress. (Alternatively, it could report absolute bytes read, and somebody else could do the math -- maybe more generally useful in the