Questions tagged [java-io]

The java.io package provides blocking input and output through data streams, serialization, and the file system.

Most applications need to process some input and produce some output based on that input. The purpose of the Java IO package (java.io) is to make that possible in Java.

The java.io package provides blocking input and output through data streams, serialization and the file system.

1753 questions
1359
votes
27 answers

How to get the current working directory in Java?

I want to access my current working directory using Java. My code: String currentPath = new java.io.File(".").getCanonicalPath(); System.out.println("Current dir:" + currentPath); String currentDir = System.getProperty("user.dir"); …
Qazi
  • 13,791
  • 3
  • 15
  • 19
217
votes
9 answers

Create a directory if it does not exist and then create the files in that directory as well

The condition is if the directory exists it has to create files in that specific directory without creating a new directory. The below code only creates a file with the new directory but not for the existing directory . For example the directory…
Sri
  • 2,193
  • 2
  • 11
  • 5
173
votes
6 answers

implements Closeable or implements AutoCloseable

I'm in the process of learning Java and I cannot find any good explanation on the implements Closeable and the implements AutoCloseable interfaces. When I implemented an interface Closeable, my Eclipse IDE created a method public void close() throws…
malas
  • 1,887
  • 3
  • 13
  • 7
150
votes
17 answers

How to list the files inside a JAR file?

I have this code which reads all the files from a directory. File textFolder = new File("text_directory"); File [] texFiles = textFolder.listFiles( new FileFilter() { public boolean accept( File file ) { return…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
138
votes
10 answers

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

How to get ddd from the path name where the test.java resides. File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
minil
  • 6,895
  • 16
  • 48
  • 55
133
votes
4 answers

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

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.…
user1454686
119
votes
4 answers

How to create a new file together with missing parent directories?

When using file.createNewFile(); I get the following exception java.io.IOException: Parent directory of file does not exist: /.../pkg/databases/mydb I am wondering is there a createNewFile that creates the missing parent directories?
Pentium10
  • 204,586
  • 122
  • 423
  • 502
118
votes
6 answers

Reading a simple text file

I am trying to read a simple text file in my sample Android Application. I am using the below written code for reading the simple text file. InputStream inputStream = openFileInput("test.txt"); InputStreamReader inputStreamReader = new…
Dalvinder Singh
  • 2,129
  • 4
  • 21
  • 20
111
votes
4 answers

how to File.listFiles in alphabetical order?

I've got code as below: class ListPageXMLFiles implements FileFilter { @Override public boolean accept(File pathname) { DebugLog.i("ListPageXMLFiles", "pathname is " + pathname); String regex =…
Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
107
votes
10 answers

java.io.Console support in Eclipse IDE

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…
Ross
  • 3,709
  • 8
  • 35
  • 33
98
votes
10 answers

Correct way to close nested streams and writers in Java

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…
dirtyvagabond
  • 1,881
  • 2
  • 19
  • 23
98
votes
10 answers

Getting java.net.SocketTimeoutException: Connection timed out in android

I'm developing an android application where I'm sending requests to the web server and parsing JSON objects. Frequently I'm getting java.net.SocketTimeoutException: Connection timed out exception while communicating with the server. Some times it…
akh
  • 2,478
  • 3
  • 23
  • 23
81
votes
6 answers

Java difference between FileWriter and BufferedWriter

What's the difference between those? I'm just learning Java ATM, but it seems like I can write to a file both ways i.e. (I didn't copy the try-catch block here.) FileWriter file = new…
Opi
  • 1,288
  • 1
  • 10
  • 14
62
votes
2 answers

How to overwrite file via java nio writer?

I try files writer as follows: String content = "Test File Content"; I used as like : Files.write(path, content.getBytes(), StandardOpenOption.CREATE); If file is not create, file is created and content is written. But If file available , the…
cguzel
  • 1,673
  • 2
  • 19
  • 34
59
votes
7 answers

Java InputStream blocking read

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…
jbu
  • 15,831
  • 29
  • 82
  • 105
1
2 3
99 100