Skip to content

Commit 8ef9d20

Browse files
author
Stephen Colebourne
committed
Add methods to read a FIle/Stream/Reader by lines
rfe 36214 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@233069 13f79535-47bb-0310-9956-ffa450edef68
1 parent 48860dc commit 8ef9d20

5 files changed

Lines changed: 202 additions & 9 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
package org.apache.commons.io;
1717

1818
import java.io.File;
19+
import java.io.FileFilter;
1920
import java.io.FileInputStream;
2021
import java.io.FileNotFoundException;
2122
import java.io.FileOutputStream;
2223
import java.io.IOException;
2324
import java.io.InputStream;
24-
import java.io.FileFilter;
2525
import java.io.OutputStream;
26+
import java.io.UnsupportedEncodingException;
2627
import java.net.URL;
2728
import java.util.Collection;
2829
import java.util.Date;
30+
import java.util.List;
2931

3032
import org.apache.commons.io.filefilter.DirectoryFileFilter;
3133
import org.apache.commons.io.filefilter.FalseFileFilter;
@@ -741,7 +743,7 @@ public static boolean waitFor(File file, int seconds) {
741743
return true;
742744
}
743745

744-
746+
//-----------------------------------------------------------------------
745747
/**
746748
* <p>
747749
* Reads the contents of a file into a String.
@@ -786,6 +788,33 @@ public static byte[] readFileToByteArray(File file) throws IOException {
786788
}
787789
}
788790

791+
/**
792+
* <p>
793+
* Reads the contents of a file line by line to a List of Strings.
794+
* </p>
795+
* <p>
796+
* There is no readLines method without encoding parameter because
797+
* the default encoding can differ between platforms and therefore results
798+
* in inconsistent results.
799+
* </p>
800+
*
801+
* @param file the file to read
802+
* @param encoding the encoding to use
803+
* @return the list of Strings representing each line in the file
804+
* @throws IOException in case of an I/O error
805+
* @throws UnsupportedEncodingException if the encoding is not supported by the VM
806+
* @since 1.1
807+
*/
808+
public static List readLines(File file, String encoding) throws IOException {
809+
InputStream in = new FileInputStream(file);
810+
try {
811+
return IOUtils.readLines(in, encoding);
812+
} finally {
813+
IOUtils.closeQuietly(in);
814+
}
815+
}
816+
817+
//-----------------------------------------------------------------------
789818
/**
790819
* <p>
791820
* Writes a String to a file creating the file if it does not exist.

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2001-2004 The Apache Software Foundation.
2+
* Copyright 2001-2005 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
2727
import java.io.Reader;
2828
import java.io.StringWriter;
2929
import java.io.Writer;
30+
import java.util.ArrayList;
31+
import java.util.List;
3032

3133
import org.apache.commons.io.output.ByteArrayOutputStream;
3234

@@ -392,6 +394,77 @@ public static String toString(byte[] input, String encoding)
392394
}
393395
}
394396

397+
// readLines
398+
//-----------------------------------------------------------------------
399+
/**
400+
* Get the contents of an <code>InputStream</code> as a list of Strings,
401+
* one entry per line, using the default character encoding of the platform.
402+
* <p>
403+
* This method buffers the input internally, so there is no need to use a
404+
* <code>BufferedInputStream</code>.
405+
*
406+
* @param input the <code>InputStream</code> to read from, not null
407+
* @return the list of Strings, never null
408+
* @throws NullPointerException if the input is null
409+
* @throws IOException if an I/O error occurs
410+
* @since 1.1
411+
*/
412+
public static List readLines(InputStream input) throws IOException {
413+
InputStreamReader reader = new InputStreamReader(input);
414+
return readLines(reader);
415+
}
416+
417+
/**
418+
* Get the contents of an <code>InputStream</code> as a list of Strings,
419+
* one entry per line, using the specified character encoding.
420+
* <p>
421+
* Character encoding names can be found at
422+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
423+
* <p>
424+
* This method buffers the input internally, so there is no need to use a
425+
* <code>BufferedInputStream</code>.
426+
*
427+
* @param input the <code>InputStream</code> to read from, not null
428+
* @param encoding the encoding to use, null means platform default
429+
* @return the list of Strings, never null
430+
* @throws NullPointerException if the input is null
431+
* @throws IOException if an I/O error occurs
432+
* @since 1.1
433+
*/
434+
public static List readLines(InputStream input, String encoding) throws IOException {
435+
if (encoding == null) {
436+
return readLines(input);
437+
} else {
438+
InputStreamReader reader = new InputStreamReader(input, encoding);
439+
return readLines(reader);
440+
}
441+
}
442+
443+
/**
444+
* Get the contents of a <code>Reader</code> as a list of Strings,
445+
* one entry per line.
446+
* <p>
447+
* This method buffers the input internally, so there is no need to use a
448+
* <code>BufferedReader</code>.
449+
*
450+
* @param input the <code>Reader</code> to read from, not null
451+
* @return the list of Strings, never null
452+
* @throws NullPointerException if the input is null
453+
* @throws IOException if an I/O error occurs
454+
* @since 1.1
455+
*/
456+
public static List readLines(Reader input) throws IOException {
457+
BufferedReader reader = new BufferedReader(input);
458+
List list = new ArrayList();
459+
String line = reader.readLine();
460+
while (line != null) {
461+
list.add(line);
462+
line = reader.readLine();
463+
}
464+
return list;
465+
}
466+
467+
//-----------------------------------------------------------------------
395468
/**
396469
* Convert the specified string to an input stream, encoded as bytes
397470
* using the default character encoding of the platform.

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.io.IOException;
2222
import java.io.OutputStream;
2323
import java.net.URL;
24+
import java.util.Arrays;
2425
import java.util.GregorianCalendar;
26+
import java.util.List;
2527

2628
import junit.framework.Test;
2729
import junit.framework.TestSuite;
@@ -665,6 +667,19 @@ public void testReadFileToByteArray() throws Exception {
665667
assertEquals(31, data[2]);
666668
}
667669

670+
public void testReadLines_Reader() throws Exception {
671+
File file = newFile("lines.txt");
672+
try {
673+
String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
674+
createLineBasedFile(file, data);
675+
676+
List lines = FileUtils.readLines(file, "UTF-8");
677+
assertEquals(Arrays.asList(data), lines);
678+
} finally {
679+
deleteFile(file);
680+
}
681+
}
682+
668683
public void testWriteStringToFile() throws Exception {
669684
File file = new File(getTestDirectory(), "write.txt");
670685
FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");

src/test/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 1999-2004 The Apache Software Foundation.
2+
* Copyright 1999-2005 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,9 +23,12 @@
2323
import java.io.FileWriter;
2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.io.Reader;
2628
import java.util.Arrays;
29+
import java.util.List;
2730

28-
import org.apache.commons.io.testtools.*;
31+
import org.apache.commons.io.testtools.FileBasedTestCase;
2932

3033
// Note: jdk1.2 dependency
3134

@@ -348,4 +351,58 @@ public void testReaderToCharArray()
348351
}
349352
}
350353

354+
//-----------------------------------------------------------------------
355+
public void testReadLines_InputStream() throws Exception {
356+
File file = newFile("lines.txt");
357+
InputStream in = null;
358+
try {
359+
String[] data = new String[] {"hello", "world", "", "this is", "some text"};
360+
createLineBasedFile(file, data);
361+
362+
in = new FileInputStream(file);
363+
List lines = IOUtils.readLines(in);
364+
assertEquals(Arrays.asList(data), lines);
365+
assertEquals(-1, in.read());
366+
} finally {
367+
IOUtils.closeQuietly(in);
368+
deleteFile(file);
369+
}
370+
}
371+
372+
//-----------------------------------------------------------------------
373+
public void testReadLines_InputStream_String() throws Exception {
374+
File file = newFile("lines.txt");
375+
InputStream in = null;
376+
try {
377+
String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
378+
createLineBasedFile(file, data);
379+
380+
in = new FileInputStream(file);
381+
List lines = IOUtils.readLines(in, "UTF-8");
382+
assertEquals(Arrays.asList(data), lines);
383+
assertEquals(-1, in.read());
384+
} finally {
385+
IOUtils.closeQuietly(in);
386+
deleteFile(file);
387+
}
388+
}
389+
390+
//-----------------------------------------------------------------------
391+
public void testReadLines_Reader() throws Exception {
392+
File file = newFile("lines.txt");
393+
Reader in = null;
394+
try {
395+
String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
396+
createLineBasedFile(file, data);
397+
398+
in = new InputStreamReader(new FileInputStream(file));
399+
List lines = IOUtils.readLines(in);
400+
assertEquals(Arrays.asList(data), lines);
401+
assertEquals(-1, in.read());
402+
} finally {
403+
IOUtils.closeQuietly(in);
404+
deleteFile(file);
405+
}
406+
}
407+
351408
}

src/test/org/apache/commons/io/testtools/FileBasedTestCase.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2001-2004 The Apache Software Foundation.
2+
* Copyright 2001-2005 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,20 +17,23 @@
1717

1818
import java.io.BufferedOutputStream;
1919
import java.io.File;
20+
import java.io.FileOutputStream;
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.io.OutputStream;
24+
import java.io.OutputStreamWriter;
25+
import java.io.PrintWriter;
2326
import java.io.Reader;
2427
import java.io.Writer;
2528
import java.util.Arrays;
2629

30+
import junit.framework.AssertionFailedError;
31+
import junit.framework.TestCase;
32+
2733
import org.apache.commons.io.FileUtils;
2834
import org.apache.commons.io.IOUtils;
2935
import org.apache.commons.io.output.ByteArrayOutputStream;
3036

31-
import junit.framework.AssertionFailedError;
32-
import junit.framework.TestCase;
33-
3437
/**
3538
* Base class for testcases doing tests with files.
3639
*
@@ -87,6 +90,22 @@ protected void generateTestData(OutputStream out, long size)
8790
}
8891
}
8992

93+
protected void createLineBasedFile(File file, String[] data) throws IOException {
94+
if (!file.getParentFile().exists()) {
95+
throw new IOException("Cannot create file " + file
96+
+ " as the parent directory does not exist");
97+
}
98+
PrintWriter output = new PrintWriter(
99+
new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
100+
try {
101+
for (int i = 0; i < data.length; i++) {
102+
output.println(data[i]);
103+
}
104+
} finally {
105+
IOUtils.closeQuietly(output);
106+
}
107+
}
108+
90109
protected File newFile(String filename) throws IOException {
91110
File destination = new File( getTestDirectory(), filename );
92111
/*

0 commit comments

Comments
 (0)