I have a text file like this:
Item 1
Item 2
Item 3
I need to be able to read each "Item X" into a string and ideally store all the strings as a vector / ArrayList.
I tried:
InputStream is = new FileInputStream("file.txt");
is.read(); //looped for every line of text
but that seems to only handle integers.
Thanks
You have several answers here, the easiest would be to us a Scanner (in java.util).
It has several convenience methods like nextLine() and next() and nextInt(), so you could simply do the following:
Scanner scanner = new Scanner(new File("file.txt"));
List<String> text = new ArrayList<String>();
while (scanner.hasNextLine()) {
text.add(scanner.nextLine());
}
Alternatively you could use a BufferedReader (in java.io):
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
List<String> text = new ArrayList<String>();
for (String line; (line = reader.readLine()) != null; ) {
text.add(line);
}
However Scanners are generally easier to work with.
You should use FileUtils to do this. It has a method named readLines
public static List<String> readLines(File file, Charset encoding) throws IOException
Reads the contents of a file line by line to a List of Strings. The file is always closed.
See #BackSlash's comment above to see how you're using InputStream.read() wrong.
#BackSlash also mentioned you can use java.nio.file.Files#readAllLines but only if you're using Java 1.7 or later.
You could use Java 7's Files#readAllLines. A short one-liner and no 3rd party library imports necessary :)
List<String> lines =
Files.readAllLines(Paths.get("file.txt"), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
String [] tmp ;
while (line != null) {
sb.append(line);
tmp = line.Split(" ");
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
Scanner scan = new Scanner(new FileInputStream("file.txt"));
scan.nextLine();
Related
I have two files:
One is a CSV file that contains the following:
Class
weka.core.Memory
com.google.common.base.Objects
client.network.ForwardingObserver
Second is a txt file that contains the following:
1_tullibee com.ib.client.ExecutionFilter
107_weka weka.core.Memory
101_netweaver com.sap.managementconsole.soap.axis.sapcontrol.HeapInfo
107_weka weka.classifiers.Evaluation
guava com.google.common.base.Objects
57_hft-bomberman client.network.ForwardingObserver
18_jsecurity org.jsecurity.web.DefaultWebSecurityManager
I would like to retrieve the lines in the txt files that contain the classes in the CSV file. To do so:
try (BufferedReader br = new BufferedReader(new FileReader("/home/nasser/Desktop/Link to Software Testing/Experiments/running_scripts/exp_23/run3/CSV/MissingClasses_RW_No_Reduction.csv"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println("==>> " + line);
Scanner scanner = new Scanner(new File("/home/nasser/Desktop/Link to Software Testing/Experiments/running_scripts/exp_23/selection (copy).txt"));
while (scanner.hasNextLine()) {
String currentLine = scanner.nextLine();
if(currentLine.contains("**>> " + line)){
System.out.println(currentLine);
}else {
System.out.println("not found");
}
}
}
}
When I run it, I get not found with all the classes in the CSV which is not the case I expect. I expect the following lines to be printed:
107_weka weka.core.Memory
guava com.google.common.base.Objects
57_hft-bomberman client.network.ForwardingObserver
How to solve that?
If you don't want the not found and the ==>> * output, just delete the corresponding lines of code
try (BufferedReader br = new BufferedReader(new FileReader("csv.txt"))) {
String line;
while ((line = br.readLine()) != null) {
Scanner scanner = new Scanner(new File("copy.txt"));
while (scanner.hasNextLine()) {
String currentLine = scanner.nextLine();
if (currentLine.contains(line)) {
System.out.println(currentLine);
}
}
scanner.close(); // added this, could use try-with but that is *advanced*
}
}
this will generate the following output, exactly as requested:
107_weka weka.core.Memory
guava com.google.common.base.Objects
57_hft-bomberman client.network.ForwardingObserver
obviously used files located in my folder...
Just my two cents: If you're using Java 8, and the CSV file is relatively small, you can simply do this:
List<String> csvLines = Files.lines(Paths.get(csvFilename))).collect(Collectors.toList());
Files.lines(Paths.get(txtFileName)))
.filter(txtLine -> csvLines.stream().anyMatch(txtLine::contains))
.forEach(System.out::println);
Python has a method lines = f.read().splitlines() by which we can read a file into list. Do we have a similar method in Java?
You can use Scanner and then read line by line and insert the line into a list
Scanner fileScanner= new Scanner(new File("yourfile.txt");
List<String> lines=new ArrayList();
while(scanner.hasNext()){
String line = scanner.next();
lines.add(line);
}
File in= new File(new URI("file://server/folder/text.txt"));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Note: The file path has to be a URI that can't contain spaces.
Files conains static metods https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
new String(Files.readAllBytes(Paths.get(path)))
.split(System.lineSeparator);
Using Scanner, i'm not sure how to read a file with multiple lines and store it all into a String. I use a loop like :
while(file.hasNext())
{
string += file.nextLine();
}
I find that the file.hasNext method eats up all of the data in the file and so file.nextInt() doesn't have any values to find and so it returns and error. What can I do to "reset" the Scanner? I tried creating a new Scanner object but that didn't change anything. I have to run this string through a method and I have run into this problem many times. What should I do?
Maybe you should try StringBuilder.
StringBuilder builder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
builder.append(line);
// process the line.
}
}
later
String text = builder.toString();
To read the entire contents of a Scanner source into a String, set the Scanner's delimiter to the end of the file:
String contents = file.useDelimiter("\\Z").next();
I am trying to read a huge file where a new line is indicated by no space, comma, new line character, or anything.
Example: line1element1, line1element2, line1element3, line2element1, line2element2, and so on..
The file is a csv and I am reading it like following:
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
String element;
String filename = "E:\\csv.csv";
Scanner scanner = new Scanner(new File(filename));
scanner.useDelimiter(",");
for (int i = 0; i < 50; i++) {
element = scanner.next();
list.add(element);
}
System.out.print(list);
}
This causes issues because the element50 in a line gets combined with element51, although it should be a new line.
Use a BufferedReader for this:
String filename = "E:\\csv.csv";
BufferedReader fileReader = null;
//Delimiter used in CSV file
final String DELIMITER = ",";
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(filename ));
//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
for(String token : tokens)
{
//Print all tokens
System.out.println(token);
}
}
USe a BufferedReader, not Scanner
File f= ...;
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.nextLine()) != null) {
String[] columns = line.split(",");
}
Why not use CSVParser from Apache Commons or OpenCSV?
Examples here:
OpenCSV Example
Apache Commons example
If you insist on doing this manually, use BufferedReader as the other comments mention.
From your description it seems your file does not have headers for each column. Use uniVocity-parsers to do this for you - it's 3 times faster than Commons CSV & OpenCSV and packed with features.
// you have many configuration options here - check the tutorial. By default values are trimmed and blank lines skipped.
CsvParserSettings settings = new CsvParserSettings();
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/your/file.csv")));
Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).
How would I read a .txt file in Java and put every line in an array when every lines contains integers, strings, and doubles? And every line has different amounts of words/numbers.
I'm a complete noob in Java so sorry if this question is a bit stupid.
Thanks
Try the Scanner class which no one knows about but can do almost anything with text.
To get a reader for a file, use
File file = new File ("...path...");
String encoding = "...."; // Encoding of your file
Reader reader = new BufferedReader (new InputStreamReader (
new FileInputStream (file), encoding));
... use reader ...
reader.close ();
You should really specify the encoding or else you will get strange results when you encounter umlauts, Unicode and the like.
Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;
List<String> lines = FileUtils.readLines(new File("untitled.txt"));
It's that easy.
"Don't reinvent the wheel."
The best approach to read a file in Java is to open in, read line by line and process it and close the strea
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console - do what you want to do
System.out.println (strLine);
}
//Close the input stream
fstream.close();
To learn more about how to read file in Java, check out the article.
Your question is not very clear, so I'll only answer for the "read" part :
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("fileName"));
String line = br.readLine();
while (line != null)
{
lines.add(line);
line = br.readLine();
}
Common used:
String line = null;
File file = new File( "readme.txt" );
FileReader fr = null;
try
{
fr = new FileReader( file );
}
catch (FileNotFoundException e)
{
System.out.println( "File doesn't exists" );
e.printStackTrace();
}
BufferedReader br = new BufferedReader( fr );
try
{
while( (line = br.readLine()) != null )
{
System.out.println( line );
}
#user248921 first of all, you can store anything in string array , so you can make string array and store a line in array and use value in code whenever you want. you can use the below code to store heterogeneous(containing string, int, boolean,etc) lines in array.
public class user {
public static void main(String x[]) throws IOException{
BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
String[] user=new String[500];
String line="";
while ((line = b.readLine()) != null) {
user[i]=line;
System.out.println(user[1]);
i++;
}
}
}
This is a nice way to work with Streams and Collectors.
List<String> myList;
try(BufferedReader reader = new BufferedReader(new FileReader("yourpath"))){
myList = reader.lines() // This will return a Stream<String>
.collect(Collectors.toList());
}catch(Exception e){
e.printStackTrace();
}
When working with Streams you have also multiple methods to filter, manipulate or reduce your input.
For Java 11 you could use the next short approach:
Path path = Path.of("file.txt");
try (var reader = Files.newBufferedReader(path)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
Or:
var path = Path.of("file.txt");
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
Or:
Files.lines(Path.of("file.txt")).forEach(System.out::println);