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;
}
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();
public void readFile() throws IOException
{
try (BufferedReader reader = new BufferedReader(new FileReader("Data.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
DefaultTableModel model = (DefaultTableModel) pracListTeacherTable.getModel();
model.addRow(new Object[]{line, line});
}
Hey everyone. I have this piece of code that essentially reads from a file called Data.txt. What I want is for each 2 lines of the text file to create 1 row on the table, however as you can see I'm using model.addRow(new Object[]{line, line}); which uses the same line for both cells of the same Row.
I need some way to store the previous line so I can have something like model.addRow(new Object[]{line, nextline}); however I can't figure out how to do it!
If anyone could help me out that would be amazing.
Update: Got it working thanks to shaoyihe!
public void readFile() throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader("Data.txt"))) {
String line1, line2;
while ((line1 = reader.readLine()) != null && (line2 = reader.readLine()) != null) {
DefaultTableModel model = (DefaultTableModel) pracListTeacherTable.getModel();
model.addRow(new Object[]{line1,line2});
}
}
What about read two lines at every loop ?
String line1, line2;
while ((line1 = reader.readLine()) != null && (line2 = reader.readLine()) != null) {
//model.addRow(new Object[]{line1, line2});
}
// for odd line
if (line1 != null) {
}
This should do it:
public void readFile() throws IOException
{
try (BufferedReader reader = new BufferedReader(new FileReader("Data.txt"))) {
String line;
String [] lines = new String[2];
int nextLine = 0;
while ((line = reader.readLine()) != null) {
System.out.println(line);
lines[nextLine++] = line;
// check if we're at the second line
if (nextLine == 2) {
DefaultTableModel model = (DefaultTableModel) pracListTeacherTable.getModel();
model.addRow(lines);
nextLine = 0;
}
}
if (nextLine == 1) {
// odd number of lines in file (error?)
// lines[0] contains the odd (last) line
// lines[1] contains the previous line (last of the previous pair)
}
}
Would it make sense to retrieve the model once before the loop? or does getModel() return a different model on each call?
I want to fill an array with names. The array should be filled depending on what gender the character is.
public void fillNameArray() throws IOException {
Character character = new Character(); //Declare and initialise character object
if(character.getGender() == "F"){
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("femaleNames.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = (String[]) lines.toArray();
}
else{
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("maleNames.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = (String[]) lines.toArray();
}
}
The problem is in the following line:
String[] array = (String[]) lines.toArray();
Write instead:
String[] array = lines.toArray(new String[lines.size()]);
See post as example.
Please can you also move the read code on a own method? So less code duplication.
public List<String> readNames(String file) {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
return lines;
}
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;
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(" ");
}