How Can I Read 2 lines From txt File - java

Hey Guys My I am Dealing With This Issue
I Want To merge to line from txt file and show them in Arraylist
So My Code Is That And I Want To Skip The -- Line To Show in Arraylist..
private List getQuotes(){
List<String> quotes = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
InputStream open = getAssets().open("barish.txt");
bufferedReader = new BufferedReader(new InputStreamReader(open));
String line;
while ((line = bufferedReader.readLine()) != null) {
quotes.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return quotes;
}
[enter image description here][1]
txt File image is here
[1]: https://i.stack.imgur.com/AiI67.png

try to use replaceAll method then read the file as above
line = line.replaceAll("--"," ");
//to remove all "--" and replace it with a space

You could check if the line is not equal to -- before adding it to the quotes list.
if(line.equals("--") == false) {
quotes.add(line);
}
Edit: Combining the lines together
In order to combine the strings between the -- lines you could accumulate the quote into a string quoteLine between each --. So if the line is not a -- line then it will be appended to the string quoteLine. If it is a -- line then it will add the previously constructed quote to the array list and initialize quoteLine to an empty string to reset it.
String line;
String quoteLine = "";
while ((line = bufferedReader.readLine()) != null) {
if(line.equals("--") == false) {
quotes.add(quoteLine);
quoteLine = "";
} else {
quoteLine += line;
}
}

enter image description here
This is Txt File ..
This Is A Poetry App.. This 2 Lines Make 1 poetry
So I Want To Display every 2 lines together In My Recyclerlist View..
This The Example...
i Want To Display My Poetry Like This In Recycler View
enter image description here

I think perhaps you want something more like this:
public List<List<String>> getQuotes() {
List<List<String>> allQuotes = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
InputStream open = getAssets().open("barish.txt");
bufferedReader = new BufferedReader(new InputStreamReader(open));
String line;
ArrayList<String> quote = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("--")) {
if (!quote.isEmpty()) {
allQuotes.add(quote);
}
quote = new ArrayList<>();
} else {
quote.add(line);
}
}
if (!quote.isEmpty()) {
allQuotes.add(quote);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return allQuotes;
}
The result is a List<List<String>>: a list of poems, with each poem in its own List<String>.

You can do something like this:
public static List<List<String>> getQuotes(String file) {
List<List<String>> allQuotes = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
// Let's have a queue backed by LinkedList to keep the two
// lines just before the next line that is '--'.
Queue<String> quotes = new LinkedList<String>();
String line;
while ((line = br.readLine()) != null) {
// If the current line starts with -- and the two previous lines persisted
// in the quotes queue to allQuotes and clear the quotes. You can do an equals
// check here if you're certain about no leading and trailing white spaces in
// each line and it is always --.
if (line.trim().startsWith("--")) {
// Don't add quotes to allQuotes when quotes queue is empty.
// You can also check quotes.size() == 2 along with !quotes.isEmpty()
// if you want to always have at least two quotes in each sub-list retuned.
if (!quotes.isEmpty()) {
allQuotes.add(new ArrayList(quotes));
quotes.clear();
}
} else if (!line.trim().isEmpty()) {
// Add each line into the quotes queue if it is not blank
// or if it is not --
quotes.add(line);
}
// If the size of queue is > 2 remove an item from from the front,
// because we are only interested in two lines before --
if (quotes.size() > 2) {
quotes.remove();
}
}
} catch (Exception e) {
e.printStackTrace(); // TODO: Handle exceptions
return null;
}
return allQuotes;
}

Related

Best way to use CSV files in Java

Im having trouble with the best approach to reading a CSV file in order to extract and compare certain things in it. The file is made up of strings, and I need to keep track if there are duplicated items. Here is what I have so far.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVReader {
public static void main(String[] args) {
String csvFile = "Cchallenge.csv";
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
} catch (IOException e) {
e.printStackTrace();
}
}
}
So I made an array called country with all the data. But when I go to print out the arrays length, it gives my a lot of different arrays with varying sizes. I am having a hard time traversing the arrays and extracting the duplicates. Any ideas will help, thanks.
If you simply wish to get a list of the items without any duplicates, then you could collect the items into a set, as sets do not allow duplicate items:
Set<String> items = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
items.addAll(Arrays.asList(line.split(cvsSplitBy)));
}
} catch (IOException e) {
e.printStackTrace();
}
If you also want to keep track of the duplicates, you could use another set and add items into it if they already exist in the first set. This would be an easy feat to accomplish, as the add method of Set returns a boolean in regards to if the set already contained the specified element or not:
Set<String> items = new HashSet<>();
Set<String> duplicates = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
for (String item : line.split(cvsSplitBy)) {
if (items.add(item)) {
continue;
}
duplicates.add(item);
}
}
} catch (IOException e) {
e.printStackTrace();
}

How to use conditional statement or contains method in a text file?

I have a java program that can read multiple files and replace values accordingly. However, I am struggling to apply a condition to it and apply the changes only when a certain condition is met. For example, if the file contains this specific character ':20:' then apply the changes otherwise leave the text file as it is.
The problem here is, since I don't have fields to look for to apply the condition accordingly I don't know how these can be applied to such a text file which contains just data like : (12345555555) 233344 100 :20:aaa.
I also looked at using the contains() method to look into the file to find the value I want then apply the changes but couldn't make it work.
public class TextFm
{
public static void main(String[] args)
{
File folder = new File("C:\\tmp");
File[] listOfFiles = folder.listFiles();
for(File file : listOfFiles)
{
replaceText(file);
}
}
public static void replaceText(File file)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while ((line = reader.readLine()) != null)
{
oldtext = oldtext + line + System.lineSeparator();
}
reader.close();
String replacedtext = oldtext.replaceAll("100", "200");
FileWriter writer = new FileWriter(file);
writer.write(replacedtext);
writer.close();
System.out.println("Done");
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Using contains() method will work fine in this case. You can do that like this:
String line = "", oldtext = "";
while ((line = reader.readLine()) != null)
{
oldtext = oldtext + line + System.lineSeparator();
}
reader.close();
if(oldtext.contains(":20:")) {
String replacedtext = oldtext.replaceAll("100", "200");
FileWriter writer = new FileWriter(file);
writer.write(replacedtext);
writer.close();
System.out.println("Done");
}
public static void replaceText(File file)
{
try
{
Charset charset = Charsets.defaulCharset();
String oldText = new String(Files.readAllBytes(file.toPath()),
charset);
if (!oldText.contains(":20")) {
return;
}
if (!oldText.matches("(?s).*you (idiot|unspeakable).*")) {
return;
}
String replacedtext = oldtext.replace("100", "200");
replacedtext = replacedtext .replaceAll("\\d", "X"); // Digit X-ed out
if (!replacedText.equals(oldText)) {
Files.write(file.toPath(), replacedText.getBytes(charset));
}
System.out.println("Done");
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
For speed one should not collect the file contents with a String and +. Better use a StringBuilder, or the very nice Files class.
Finding text goes either by contains or by a regular expression match.
Replacement can be done by either too.
The replace, replaceFirst and replaceAll methods return the original string when nothing could be replaced.
Regex (?s) lets . (=any char) also match line breaks.

Reading from a text file with spaces and then putting into arrayList

I have spent the last week trying to figure out how to make this stupid code work. I have managed to get everything to work except for reading from my text file. It can read an individual integer on a line, but when given a line with multiple integers separated by spaces, it freaks out. Now I've gone and tried to fix it and the code won't even compile anymore. Only one line is causing problems.
I'm not good at coding, so I don't know where to begin. Yes, I've looked this up online. Yes, I've checked the forums. Yes, I have tried multiple different methods to make this work....
How do I fix this?? :(
ArrayList<Integer> list = new ArrayList<Integer>();
// the above line is in a different method in the same class, but it's relevant here
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
// I want the following line to read "218 150 500 330", and to store each individual integer into the list. I don't know why it won't work :(
list.add(Integer.parseInt(src.next().trim()));
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//print out the list
System.out.println(list);
Thank you for the help! I'm sure that I'm just missing something really simple...
You can use a Scanner(String) like
while ((text = reader.readLine()) != null) {
Scanner scanner = new Scanner(text);
while (scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
}
Of course, your entire method could be simplified by using a try-with-resources Statement and the diamond operator and just Scanner(File) like
public static void main(String[] args) {
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
List<Integer> list = new ArrayList<>();
try (Scanner scanner = new Scanner(file);) {
while (scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
} catch (Exception e) {
e.printStackTrace();
}
// print out the list
System.out.println(list);
}
Do this inside the while loop
String[] individualArray = text.split(" ");//note space
for(String individual:individualArray){
yourList.add(individual);//You need to parse it to integer here as you have already done
}
In the above code, individualArray will contain each individual integers that are separated by space. And inside the for loop each string needs to be parsed to integer and then added to your list
try this :
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
// you need only this for loop in you code.
for (String value : text.split(" ")) { // get list of integer
if(!value.equals("")) // ignore space
list.add(Integer.parseInt(value)); // add to list
}
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
// print out the list
System.out.println(list);
}

Reading from a text file in java is returning some garbage value

I'm performing certain commands through command prompt and storing the values in a text file.
wmic logicaldisk where drivetype=3 get deviceid > drive.txt
Now I want to read the string stored in the text file from my java file. When I try to do this:
try {
File file = new File("drive.txt");
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
int i=0;
while ((string[i] = in.readLine()) != null) {
System.out.println(string[i]);
++i;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
I get the output as follows:
ÿþD[]E[]V[]I[]C[]E[]
how to avoid this?
while ((string[i] = in.readLine()) != null) {
System.out.println(string[2]);
}
over there you are missing the i++;
However I would advise you to use this structure: Use a ArrayList instead of an array, since this allows you to have a self-resizing structure, also instead in the while use the method ready(); from the BufferedRead in order to check the end from the document, at the end the for it's just to display the elements in String ArrayList.
ArrayList<String> string = new ArrayList<String>();
try {
File file = new File("drive.txt");
BufferedReader entrada;
entrada = new BufferedReader(new FileReader(file));
entrada.readLine();
while (entrada.ready()) {
string.add(entrada.readLine());
}
entrada.close();
} catch (IOException e) {
e.printStackTrace();
}
for (String elements : string) {
System.out.println(elements);
}
Why do you need a string array here? The size of the array may be wrong? Simply use a string instead of array. I tried this and works fine for me:
try {
String string;
File file = new File("drive.txt");
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
int i = 0;
while ((string = in.readLine()) != null) {
System.out.println(string);
++i;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
If you are using eclipse IDE, change the encoding type. Go to Edit->Set Encoding-> Others->UTF-8.

Why is this BufferedReader returning empty String instead of null?

I have a BufferedReader iterating through the lines of a CSV file; when it gets to the end of the file, it returns the following error:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
How do I get the reader to realize it reached the end of file and input is null instead of empty? I've checked the file and there is no whitespace at the end of the last line.
Code:
File filReadMe = new File(inputFile);
BufferedReader brReadMe = new BufferedReader(new InputStreamReader(new FileInputStream(filReadMe), "UTF-8"));
try
{
String strLine;
while ((strLine = brReadMe.readLine()) != null)
{
System.out.println(strLine);
//place the line into CsvRecordFactory
int record = csv.processLine(strLine, input);
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
//Close the BufferedReader
try {
if (brReadMe != null)
brReadMe.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
You can simply modify your code this way:
while ((strLine = brReadMe.readLine()) != null)
{
if (!strLine.isEmpty()) {
System.out.println(strLine);
//place the line into CsvRecordFactory
int record = csv.processLine(strLine, input);
}
}
This way, your code will ignore all empty lines, not only those at the end of your file.
The problem isn't the end of file. The problem is that you are processing a blank line as though it wasn't blank. That could conceivably happen anywhere, not just as a final line before EOF. Check the line for emptiness before you start parsing it.
if the end of your file is a newline, try backspacing it to the end of the previous line and see if you still get the error
Try to give it a small if condition to check whether the string is empty or not.
Version 1: Check if string is empty
while ((strLine = brReadMe.readLine()) != null)
{
if(!strLine.isEmpty()){
System.out.println(strLine);
//place the line into CsvRecordFactory
int record = csv.processLine(strLine, input);
}
}
I also have version 2 of code, which checks if string is number otherwise if they are characters or empty or anything else, the code inside if is going be ignored
while ((strLine = brReadMe.readLine()) != null)
{
if(strLine.matches("\\d+")){
System.out.println(strLine);
//place the line into CsvRecordFactory
int record = csv.processLine(strLine, input);
}
}

Categories