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;
Related
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);
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 a file containing following text:
CompanyName=IBM
Name=User1234
CompanyName=Google
How can check if a text 'CompanyName' exists in a text file, if it does then select a string after = operator?. So the text from above file should output IBM and Google.
while ((line = br.readLine()) != null) {
System.out.println(line);
}
You can use java.util.Properties
Properties properties = new Properties();
properties.load(new InputStreamReader
(new FileInputStream("myprops.properties"), StandardCharsets.UTF_8));
String property = properties.getProperty("CompanyName");
Would it be something like this?
while ((line = br.readLine()) != null) {
System.out.println(line);
if(line.contains("CompanyName"))
{
String[] splits = line.split("=");
String desired = splits[1]; // second position in the string array.
}
}
EDIT: adding more functionality in getting the other fields at the same time.
while ((line = br.readLine()) != null)
{
System.out.println(line);
String compName = "";
String name = "";
if(line.contains("CompanyName"))
{
String[] splits2 = line.split("=");
compName = splits2[1]; // second position in the string array.
if((line = br.readLine()) != null)
{
String[] splits = line.split("=");
name = splits[1];
}
}
}
Here is how you can do it:
function findValueOfAttr(String attr, br) {
while ((line = br.readLine()) != null) {
if (line.indexOf(attr) == 0)
return line.subString(line.indexOf("=")+1);
}
return false;
}
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 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(" ");
}