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
24
votes
7 answers

Java I/O streams; what are the differences?

java.io has many different I/O streams, (FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedStreams... etc.) and I am confused in determining the differences between them. What are some examples where one stream type is preferred…
Lawrence
  • 265
  • 1
  • 3
  • 8
23
votes
8 answers

Java: accessing a List of Strings as an InputStream

Is there any way InputStream wrapping a list of UTF-8 String? I'd like to do something like: InputStream in = new XyzInputStream( List lines )
Marc Polizzi
  • 9,275
  • 3
  • 36
  • 61
23
votes
7 answers

Converting transparent gif / png to jpeg using java

I'd like to convert gif images to jpeg using Java. It works great for most images, but I have a simple transparent gif image: Input gif image http://img292.imageshack.us/img292/2103/indexedtestal7.gif [In case the image is missing: it's a blue…
asalamon74
  • 6,120
  • 9
  • 46
  • 60
23
votes
3 answers

How to get files from resources folder. Spring Framework

I'm trying to unmarshal my xml file: public Object convertFromXMLToObject(String xmlfile) throws IOException { FileInputStream is = null; File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml"))); try…
Tom Wally
  • 542
  • 3
  • 8
  • 20
22
votes
3 answers

How to find sub-directories in a directory/folder?

I'm looking for a way to get all the names of directories in a given directory, but not files. For example, let's say I have a folder called Parent, and inside that I have 3 folders: Child1 Child2 and Child3. I want to get the names of the folders,…
iaacp
  • 4,655
  • 12
  • 34
  • 39
21
votes
7 answers

How can i zip files in Java and not include files paths

For example, I want to zip a file stored in /Users/me/Desktop/image.jpg I made this method: public static Boolean generateZipFile(ArrayList sourcesFilenames, String destinationDir, String zipFilename){ // Create a buffer for reading the…
Ignacio
  • 213
  • 1
  • 2
  • 4
21
votes
5 answers

what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object

This may be a silly one, but I want to know the background operation difference. InputStream is = new FileInputStream(filepath); FileInputStream is = new FileInputStream(filepath); What is the difference between the above two lines of code and in…
Mathan Kumar
  • 634
  • 2
  • 7
  • 19
20
votes
6 answers

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
Aly
  • 15,865
  • 47
  • 119
  • 191
20
votes
1 answer

java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows

I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error: Exception in thread "main" java.lang.UnsupportedOperationException:…
mathlearner
  • 7,509
  • 31
  • 126
  • 189
19
votes
4 answers

Choosing between FileReader and InputStreamReader

I have two methods to read Text File In java one using FileReader and Other File InputStream FileReader fr=new FileReader("C:\\testq\\test.txt"); BufferedReader br=new BufferedReader(fr); String s; while((s=br.readLine())!=null){ …
Basimalla Sebastin
  • 1,159
  • 3
  • 9
  • 15
19
votes
4 answers

how to create a file with world readable permission under subdirectory of files directory

I need to create files under myapp/files/subdir with global permission in my application. I do this because I use external applications to open some files Using this FileOutputStream fos = openFileOutput(FILENAME,…
ikbal
  • 1,844
  • 4
  • 26
  • 46
19
votes
5 answers

java.nio.file.Files.delete(Path path) - occasional failure to recursively delete directory using SimpleFileVisitor

Trying to troubleshoot an occasional java.nio.file.DirectoryNotEmptyException in a recursive delete method taken from Delete directories recursively in Java Code (credit to @TrevorRobinson) : static void removeRecursive(Path path) throws IOException…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
18
votes
4 answers

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

What is the best way to write bytes in the middle of a file using Java?
jjnguy
  • 136,852
  • 53
  • 295
  • 323
16
votes
2 answers

DataInputStream.read() vs DataInputStream.readFully()

Im making a simple TCP/IP Socket app Whats the different between doing this: DataInputStream in = new DataInputStream(clientSocket.getInputStream()); byte[] buffer = new byte[100]; in.readFully(buffer); versus doing this: DataInputStream in = new…
Krimson
  • 7,386
  • 11
  • 60
  • 97
16
votes
2 answers

InputStream, mark(), reset()

How are mark() and reset() methods working exactly(in code below), step by step ? I tried to write my own example but is starts to throw wrong mark exception or similar to that, and I cannot understand what is the point of placing mark and reset…
ashur
  • 4,177
  • 14
  • 53
  • 85