java-io

Fastest way of processing Java IO using ASCII lines

Hello, I'm working with an ASCII input/output stream over a Socket and speed is critical. I've heard using the right Java technique really makes a difference. I have a textbook that says using Buffers is the best way, but also suggests chaining with DataInputStreamReader. For output I'm using a BufferedOutputStream with OutputStreamWri...

Java File manipulation

So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to u...

Resetting the InputStream of a Java Socket after reaching EOS.

I'm attempting to connect two separate applications via a TCP/IP socket. In this case neither of the applications is running as a "client" of the other, it would be easier to describe them as two separate servers who need to communicate with each other. To receive the data I'm using the InputStream.Read() function and once it receives a...

java IO to copy one File to another

I have two Java.io.File objects file1 and file2. I want to copy the contents from file1 to file2. Is there an standard way to do this without me having to create a method that reads file1 and write to file2 ...

Directory listing not refreshing

This is driving me crazy! I have a panel that displays a list of files from a directory. The list is stored in a vector. When I click on a button, a new file is created in the same directory and the list needs to be refreshed. I don't understand why Java cannot see the new file, even though if I add a good old DIR in Dos, Dos can see t...

Java: opening and reading from a file without locking it.

I need to be able to mimic 'tail -f' with Java. I'm trying to read a log file as it's being written by another process, but when I open the file to read it, it locks the file and the other process can't write to it anymore. Any help would be greatly appreciated! Here is the code that I'm using currently: public void read(){ Scanner...

Read Lines between a start index and end index in java

Lets say I have 10 lines in a file. I have 2 parameters that specify the beginning and ending of a index. StartIndex = 2 // specifies the first 2 lines EndIndex = 3 // specifies the last 3 lines I need to read the lines in between. I know maintaining index and skipping is one of the ways...but are there any other efficient ways (even ...

Java binary files writeUTF... explain specifications...

I'm studying Java on my own. One of the exercises is the following, however I do not really understand what it is asking to.... any smart java gurus out there that could explain this in more detail and simple words? Thanks Suppose that you have a binary file that contains numbers whos type is either int or double. You dont know the orde...

Determine whether a folder is present?

How would I go about determining whether a file or directory has been created in Java? I basically want to create a data directory if one is not already present. Thanks. ...

Is there a java library / package analogous to <stdio.h>?

I have been doing Java on and off for about 14 years, and almost nothing else the last 6 years or so. I really hate the java.io package -- its legion of subclasses and adapters. I do like exceptions, rather than having to always poll "errno" and the like, but I could surely live without declared exceptions. Is there anything that fu...

SCJP Book, IO section: Is this a typo or is there a reason it would look like this?

My question is about line (edit: 19), where the new PrintWriter is created with the constructor taking the FileWriter fw as a parameter. I don't understand the use of chaining the BufferedWriter bw to FileWriter if it isn't used later on in the actual writing. Can Java apply chaining in a way that bw still somehow affects the rest of the...

Editing a 1MB file continuously, what's more efficient?

I've to be continuously editing a 1MB file, simulating a file system. I've to modify the directory of File Control Blocks, FAT, blocks, etc. Proffesor recommended overwriting the file every time an update is made. 1MB shouldn't take minutes to do that, but I don't like this way. Is it a FileChannel the way to go here? Also, I understan...

Modify a hidden file in Java

I have a file that user downloads and then I execute a command within java to hide the file: Runtime.getRuntime().exec("attrib +H myFile.txt"); Now later I need to access that hidden file but I'm getting java.io.FileNotFoundException: myFile.txt (Access is denied) This works if file is not hidden, but the file needs to be hidden s...

Shouldn't ObjectInputStream extend FilterInputStream?

The block quotes are from the Java Docs - A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. A DataInputStream lets an application read primitive Java data types from an underlying input ...

Windows temporary file in Java

How to create a file in Windows that would have attributes FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE set using Java? I do want my file to be just in-memory file. To precise: delete-on-exit mechanism does not satisfy me, because I want to avoid situation, when some data leaves on disk in case, for example, application crash...

FileReader not found in Java ME

The class java.io.FileReader not found in Java ME. I need this in order to get the file and then parse it with an xml parser. Anyone know any alternatives for this class? *added using CLDC profile. The xml file to be read is in the JAR. ...

Lackadaisical One-to-One between Char and Byte Streams

I expected to have a one-to-one correspondence between the character streams and byte streams in terms of how the classes are organized in their hierarchy. FilterReader and FilterWriter (character streams) correspond back to FilterInputStream and FilterOutputStream (byte stream) classes. However I noticed few changes as - BufferedIn...

Reading files with Java

I would like to know how can I read a file byte by byte then perform some operation every n bytes. for example: Say I have a file of size = 50 bytes, I want to divide it into blocks each of n bytes. Then each block is sent to a function for some operations to be done on those bytes. The blocks are to be created during the read process ...

Read File/Directory properties with java

How can I read the file information (for example size, line count, last modification, etc) from a file in the file-system or the directory content with JAVA? I need it for a linux operating system. Thanks Ps. This is my first question, althought I have user this forum for a while so please be kind :P ...

how to use append function in file

I want to write in a file but in a way that it should not delete existing data in that file rather it should append that file. Can anybody please help by giving any example related to appending a file? Thank you ...