I need to know if I am in the last line of my CSV file or no.
My code is like this:
BufferedReader br = new BufferedReader(new FileReader(MyCSVFile));
while ((line = br.readLine()) != null) {
// Doing some actions
//I need to define here; If it is the last row, do another action
}
Can anyone help me with this?
Thanks
Sin
You could do something like this:
String lastLine = "";
while ((line = br.readLine()) != null) {
// Doing some actions
// Overwrite lastLine each time
lastLine = line;
}
// now do something with lastLine
System.out.println("This is the last line: " + lastLine);
Related
What is the best way to replace a string in a while loop that reads lines from a file?
while ((line = reader.readLine()) != null) {
if (line.contains("blabla")) {
line = line.replaceAll("blabla", "eee");
}
writer.write(line);
}
is this correct way, I want to read all file lines, and check each line if contains this word, there is only one line that contains this word, if it contains it then replace this word and do not check another lines.
You can use a boolean flag as a condition in your while and update it in your if:
boolean found = false;
while ((!found) && ((line = reader.readLine()) != null)) {
if (found = line.contains("blabla")) {
line = line.replaceAll("blabla", "eee");
}
writer.write(line);
}
You can use replace this,
File your_file = new File(filePath)
String oldContent = “”;
BufferedReader reader = new BufferedReader(new FileReader(your_file));
String line = reader.readLine();
while (line != null)
{
oldContent = oldContent + line + System.lineSeparator();
line = reader.readLine();
}
String newContent = oldContent.replaceAll("wantToBeReplace", newString);
FileWriter writer = new FileWriter(your_file);
writer.write(newContent);
reader.close();
writer.close();
I have the following code, which read thru the files and search for the line that start with subject and print out that line. But I only want to read the first line that contain the 'Subject' but currently it getting all the line that start with 'Subject' how can I configure such that it search for the first 'Subject' and print that one line ?
br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("Subject ")) {
System.out.println(line);
}
}
Use break; statement,
br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("Subject ")) {
System.out.println(line);
break;
}
}
You should try to use Regular Expressions to find all the occurrences of desired
Pattern i.e. something like "Subject*\n" in your case.
Check out this very good tutorial on Regex: www.vogella.com/tutorials/JavaRegularExpressions/article.html
You are not stopping your loop, so it will fetch until last line;
try this
if (line.startsWith("Subject ")) {
System.out.println(line);
break; // break the lop on success match
}
Using ArrayList you can save each line start with "Subject" and can traverse all of them you can use it according to your requirements
ArrayList<String> list = new ArrayList<String>();
br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("Subject ")) {
list.add(line);
}
}
System.out.println(list.get(0);
I am trying to read through a file and find the first line that contains the string "ua, ". I want to set that line to be equal to sCurrentLine. After that, I then want to set the next line in the file that contains "ua, " to be equal to sNextLine. Then I will process those line, and after that I want sCurrentLine to be equal to sNextLine and then the loop continues till the end of the file.
This is what I have so far
while ((line = br.readLine()) != null)
{
if (line.contains("ua, "))
{
sCurrentLine = line;
//now I don't know what to do
boolean currentLineSet = false;
while ((line = br.readLine()) != null)
{
if (line.contains("ua, "))
{
if (!currentLineSet) {
sCurrentLine = line;
currentLineSet = true;
} else {
sNextLine=line;
//processing
sCurrentLine = sNextLine;
}
}
You should use an ArrayList which will contain Strings.
ArrayList<String> st = new ArrayString<String>();
while ((line = br.readLine()) != null)
{
if (line.contains("ua, "))
{
st.add(line);
}
}
Since you don't know the number of lines that will contain the "ua, " String, you should use an ArrayList.
Now you'll have the lines that contains "ua, " in the ArrayList st.
OP EDIT
If you want to process two lines, you can save the first line you find with "ua, " and then read another line, check if it contains this String:
String st1, st2;
while ((line = br.readLine()) != null)
{
if (line.contains("ua, "))
{
st1 = line;
}
if ((line = br.readLine()) != null && line.contains("ua, "))
{
st2 = line;
}
}
Of course you can set flags to see whether the first and second lines contained the String.
int index, iCurrentLine = 1;
while ((line = br.readLine()) != null)
{
index++;
if (line.contains("ua, ")) {
sCurrentLine = line;
iCurrentLine = index;
}
}
Loop through the file contents again, then make sure that the new index doesn't equal the previous index, then just set sNextLine = line;
My problems is that I have to arrange, when searching for a customer, arrange the while loop to only examine every fourth line read.
This is the code I already have on this problem:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
}
br.close();
Does anybody know what needs to be at the place of "..."?
Thanks!
Something along the lines of
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i % 4 == 0)
{
// if i is divisible by 4, then
// your actual code will get executed
...
}
}
Just call br.readLine() 3 times at the end of the loop, discarding the output:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
for(int i=0;i<3;i++){ br.readLine(); }
}
br.close();
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
int count 0;
while ((line = br.readLine()) != null)
{
if (count!=3)
count++;
else {
// Do something?
count=0;
}
}
br.close();
I have some code to read the lines from a file, I would like to recognize when the line starts or the fisrt character (not blank) is '*' and ignore it, so inside the while statement add something like
if(line[0]=='*')
ignore that line
I have something like:
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
while ((line = input.readLine()) != null) {
String[] words = line.split(" ");
....
}
How to complete the code?
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
while ((line = input.readLine()) != null) {
if(line.trim().indexOf('*') == 0)
continue;
String[] words = line.split(" ");
....
}
Change your while loop as:
while((line = input.readLine()) != null){
if(!line.startsWith("*")){
String[] words = line.split(" ");
....
}
}
EDIT
If "*" is not in the beginning of the line but at some position in the line, use the following
if(line.indexOf("*") == position){
......
}
where position can be an integer specifying the position you are interested in.
Assuming the * marks and end of line comment, this loop will keep anything needed on the line before it.
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
char[] lineChars;
while ((line = input.readLine()) != null) {
lineChars = line.toCharArray();
line = "";
for(char c: lineChars){
if(c == '*')break;
line.concat(Character.toString(c));
}
if(!line.equals(""))
String[] words = line.split(" ");
}