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
11
votes
4 answers

Read file with whitespace in its path using Java

I am trying to open files with FileInputStream that have whitespaces in their names. For example: String fileName = "This is my file.txt"; String path = "/home/myUsername/folder/"; String filePath = path + filename; f = new BufferedInputStream(new…
user253530
  • 2,583
  • 13
  • 44
  • 61
11
votes
3 answers

java : writing large files?

Greetings , I get huge number of records from database and write into a file.I was wondering what the best way to write huge files. (1Gb - 10Gb). Currently I am using BufferedWriter BufferedWriter mbrWriter=new BufferedWriter(new…
Ashika Umanga Umagiliya
  • 8,988
  • 28
  • 102
  • 185
11
votes
5 answers

How java.io.Buffer* stream differs from normal streams?

1) How does buffered streams work in background, how do they differ from normal streams and what are the advantage(s) of using them? 2) DataInputStream is also Byte based. But it is having methods to readLine(). What's the point in here?
i2ijeya
  • 15,952
  • 18
  • 63
  • 72
10
votes
1 answer

Return File From Resteasy Server

Hi, I wanted to return a file from a resteasy server. For this purpose, I have a link at the client side which is calling a rest service with ajax. I want to return the file in the rest service. I tried these two blocks of code, but both didn't…
Sedat Başar
  • 3,740
  • 3
  • 25
  • 28
10
votes
6 answers

Alternative to File.exists() in Java

I never thought it would happen to me, but I ran into my first bug in Java: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595 I'm pretty much in the same exact situation as described in the bug (NFS on linux), and I'm seeing that…
Ben
  • 7,692
  • 15
  • 49
  • 64
10
votes
6 answers

Resource leak: 'in' is never closed, though it IS closed

I know that there are a couple of similarly entitled questions out there, but most of them have simply forgotten to put a close() directive on their stream. This here is different. Lets say I have the following minimal example: public void test()…
theV0ID
  • 4,172
  • 9
  • 35
  • 56
10
votes
4 answers

File.mkdir() and mkdirs() are creating file instead of directory

I use the following code: final File newFile = new File("/mnt/sdcard/test/"); newFile.mkdir(); // if I use mkdirs() result is the same And it creates an empty file! Why?
artem
  • 16,382
  • 34
  • 113
  • 189
9
votes
6 answers

What is wrong with FileInputStream.read(byte[])?

In response to my answer to a file-reading question, a commenter stated that FileInputStream.read(byte[]) is "not guaranteed to fill the buffer." File file = /* ... */ long len = file.length(); byte[] buffer = new byte[(int)len]; FileInputStream…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
9
votes
3 answers

How to copy large data files line by line?

I have a 35GB CSV file. I want to read each line, and write the line out to a new CSV if it matches a condition. try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("source.csv"))) { try (BufferedReader br =…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
9
votes
2 answers

Try-with-resources close order

I am looking at examples of try-with-resources in Java and I understand the following one: try (Connection conn = DriverManager.getConnection(url, user, pwd); Statement stmt = conn.createStatement(); ResultSet rs =…
Aliuk
  • 1,249
  • 2
  • 17
  • 32
9
votes
3 answers

Ways to proxy an InputStream

I am using Android-Universal-Image-Loader to load images from remote server over HTTPS on my Android application. To have access to images the client should provide a valid token and sometimes server can return "expired crsf token" error. In order…
bvk256
  • 1,837
  • 3
  • 20
  • 38
9
votes
4 answers

Is PrintWriter buffered?

I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance. But I tried something like this, PrintWriter pw = new PrintWriter(System.out); pw.println("Statement…
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
9
votes
2 answers

Reading file from PATH of URI

I have to read a file which can be local or in remote. The file path or URI will come form user input. Currently I am reading only local file,this way public String readFile(String fileName) { File file=new File(fileName); BufferedReader…
Saif
  • 6,804
  • 8
  • 40
  • 61
9
votes
3 answers

Java FileOutputStream consecutive close takes a long time

I'm facing a little weird situation. I'm copying from FileInputStream to FileOutputStream a file that is sized around 500MB. It goes pretty well (takes around 500ms). When I close this FileOutputStream the FIRST time, it takes about 1ms. But here…
zdenda.online
  • 2,451
  • 3
  • 23
  • 45
9
votes
2 answers

OutputStream.write() successful but the data is not delivered

I have a very strange behaviour during writing to a socket. In my mobile client I'm using a socket which is initialized as follows: private void initSocket() { socket = new Socket(); socket.connect(new InetSocketAddress(host, port)); os…
Valelik
  • 1,743
  • 2
  • 18
  • 22