java-io

Using java.io library in eclipse so FileInputStream can read a dat file

て烟熏妆下的殇ゞ 提交于 2019-11-26 14:53:02
问题 Goal : Print the data from a .dat file to the console using Eclipse. (Long-Term Goal) : Executable that I can pass a .dat file to and it creates a new txt file with the data formatted. The .dat : I know the .dat file contains control points that I will need to create a graph with using ECMAScript. Eclipse Setup: Created Java Project New > Class .. called the Class FileRead Now I have FileRead.java which is: 1/ package frp; 2/ 3/ import java.io.BufferedReader; 4/ import java.io.File; 5/ import

How to create a new java.io.File in memory? [closed]

我只是一个虾纸丫 提交于 2019-11-26 13:21:26
问题 How can I create new File (from java.io ) in memory, not on the hard disk? I am using the Java language. I don't want to save the file on the hard drive. I'm faced with a bad API ( java.util.jar.JarFile ). It's expecting File file of String filename . I have no file (only byte[] content) and can create temporary file, but it's not beautiful solution. I need to validate the digest of a signed jar. byte[] content = getContent(); File tempFile = File.createTempFile("tmp", ".tmp");

Correct way to close nested streams and writers in Java [duplicate]

删除回忆录丶 提交于 2019-11-26 12:51:39
This question already has an answer here: Is it necessary to close each nested OutputStream and Writer separately? 7 answers Note: This question and most of its answers date to before the release of Java 7. Java 7 provides Automatic Resource Management functionality for doing this easilly. If you are using Java 7 or later you should advance to the answer of Ross Johnson . What is considered the best, most comprehensive way to close nested streams in Java? For example, consider the setup: FileOutputStream fos = new FileOutputStream(...) BufferedOS bos = new BufferedOS(fos); ObjectOutputStream

Streaming large files in a java servlet

随声附和 提交于 2019-11-26 12:11:30
I am building a java server that needs to scale. One of the servlets will be serving images stored in Amazon S3. Recently under load, I ran out of memory in my VM and it was after I added the code to serve the images so I'm pretty sure that streaming larger servlet responses is causing my troubles. My question is : is there any best practice in how to code a java servlet to stream a large (>200k) response back to a browser when read from a database or other cloud storage? I've considered writing the file to a local temp drive and then spawning another thread to handle the streaming so that the

What does System.in.read actually return?

半世苍凉 提交于 2019-11-26 07:47:24
问题 What does : System.in.read() return ? The documentation says : Returns: the next byte of data, or -1 if the end of the stream is reached. But for example if I enter : 10 I get back 49 . Why is that ? 回答1: 49 is the ASCII value of the char 1 . It is the value of the first byte. The stream of bytes that is produced when you enter 1 0 Enter on your console or terminal contains the three bytes {49,48,10} (on my Mac, may end with 10,12 or 12 instead of 10, depending on your System). So the output

Java InputStream blocking read

走远了吗. 提交于 2019-11-26 05:57:29
问题 According to the java api, the InputStream.read() is described as: If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. I have a while(true) loop doing a read and I always get -1 when nothing\'s sent over the stream. That\'s expected. My question is when would read() ever block? Since if it doesn\'t get any data it returns -1. I would

Best Way to Write Bytes in the Middle of a File in Java

风格不统一 提交于 2019-11-26 05:36:31
问题 What is the best way to write bytes in the middle of a file using Java? 回答1: Reading and Writing in the middle of a file is as simple as using a RandomAccessFile in Java. RandomAccessFile, despite its name, is more like an InputStream and OutputStream and less like a File . It allows you to read or seek through bytes in a file and then begin writing over whichever bytes you care to stop at. Once you discover this class, it is very easy to use if you have a basic understanding of regular file

How to get just the parent directory name of a specific file

空扰寡人 提交于 2019-11-26 04:44:48
问题 How to get ddd from the path name where the test.java resides. File file = new File(\"C:/aaa/bbb/ccc/ddd/test.java\"); 回答1: Use File's getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory. Mark's comment is a better solution than lastIndexOf() : file.getParentFile().getName(); These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File ). When getParentFile() is null you'll need to

Correct way to close nested streams and writers in Java [duplicate]

自作多情 提交于 2019-11-26 03:07:26
问题 This question already has answers here : Is it necessary to close each nested OutputStream and Writer separately? (7 answers) Closed 3 years ago . Note: This question and most of its answers date to before the release of Java 7. Java 7 provides Automatic Resource Management functionality for doing this easilly. If you are using Java 7 or later you should advance to the answer of Ross Johnson. What is considered the best, most comprehensive way to close nested streams in Java? For example,

java.io.Console support in Eclipse IDE

被刻印的时光 ゝ 提交于 2019-11-26 01:44:43
问题 I use the Eclipse IDE to develop, compile, and run my Java projects. Today, I\'m trying to use the java.io.Console class to manage output and, more importantly, user input. The problem is that System.console() returns null when an application is run \"through\" Eclipse. Eclipse run the program on a background process, rather than a top-level process with the console window we\'re familiar with. Is there a way to force Eclipse to run the program as a top level process, or at least create a