java bufferreader read elements equal to another array elements - java

I read a file by using bufferreader and also have another array(array_to_compare_with). So, I want to compare these two data.
How can I get over this ? Anybody has a clue ?
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// strLine and array_to_compare_with comparison needed to be done
}

Maybe you want something like this:
String[] array_to_compare_with = { "str1","str2","str3","str4" };
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
boolean found = false;
for(String str : array_to_compare_with){
if(str.equalsIgnoreCase(strLine.trim())){
found = true;
break;
}
}
if(found){
//DO WHAT YOU WHANT
}
}

String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
for(String s: array_to_compare_with){
if(strLine.contains(s)){
System.out.println(strLine);
break;
}
}
}

Related

how to get the data from file and set to text Area in vaadin

I have Created One project.Buta i get data in console i want set data to textarea
File[] F=File.listFiles();
for (File File1:F) {
FileInputStream fstream = null;
String strLine ;
try {
fstream = new FileInputStream(File1);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((strLine = br.readLine()) != null)
System.out.println (strLine);
String str=strLine;
final TextArea txt=new TextArea(str);
layout.addComponents(txt);
//br.close();
} catch (IOException e) {
e.printStackTrace();
}
You must accumulate the read lines, so you can then later on add it to the text area.
You might consider what to do with line breaks, currently they just get sorted out of the final string/text.
for (File File1:F)
{
FileInputStream fstream = null;
String strLine;
StringBuilder sb= new StringBuilder();
try
{
fstream = new FileInputStream(File1);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((strLine = br.readLine()) != null)
{
System.out.println (strLine);
sb.append(str);
}
final TextArea txt=new TextArea(sb.toString());
layout.addComponents(txt);
//br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

Reading contents after splitting

My problem is that I have a code and I have a problem of fixing the "Cannot find symbol" error. Here's the code.
public static void writer() throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
in = new FileReader("Student.txt");
out = new FileWriter("StudentAvg.txt");
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t");
The error is pointed on the splitting process. Is there a problem with my variables?
The problem is, that you haven't read any lines.
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t"); // <-- this is your input file reader.
I think you wanted
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
You might also use a try-with-resources and something like
try (BufferedReader br = new BufferedReader(//
new FileReader("Student.txt"));
BufferedWriter bw = new BufferedWriter(//
new FileWriter("StudentAvg.txt"))) {
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
}
}

BufferedReader to skip first line

I am using the following bufferedreader to read the lines of a file,
BufferedReader reader = new BufferedReader(new FileReader(somepath));
while ((line1 = reader.readLine()) != null)
{
//some code
}
Now, I want to skip reading the first line of the file and I don't want to use a counter line int lineno to keep a count of the lines.
How to do this?
You can try this
BufferedReader reader = new BufferedReader(new FileReader(somepath));
reader.readLine(); // this will read the first line
String line1=null;
while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
//some code
}
You can use the Stream skip() function, like this:
BufferedReader reader = new BufferedReader(new FileReader(somepath));
Stream<String> lines = reader.lines().skip(1);
lines.forEachOrdered(line -> {
...
});
File file = new File("path to file");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine()) != null) { // read through file line by line
if(count != 0) { // count == 0 means the first line
System.out.println("That's not the first line");
}
count++; // count increments as you read lines
}
br.close(); // do not forget to close the resources
Use a linenumberreader instead.
LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));
String line1;
while ((line1 = reader.readLine()) != null)
{
if(reader.getLineNumber()==1){
continue;
}
System.out.println(line1);
}
You can create a counter that contains the value of the starting line:
private final static START_LINE = 1;
BufferedReader reader = new BufferedReader(new FileReader(somepath));
int counter=START_LINE;
while ((line1 = reader.readLine()) != null) {
if(counter>START_LINE){
//your code here
}
counter++;
}
You can do it like this:
BufferedReader buf = new BufferedReader(new FileReader(fileName));
String line = null;
String[] wordsArray;
boolean skipFirstLine = true;
while(true){
line = buf.readLine();
if ( skipFirstLine){ // skip data header
skipFirstLine = false; continue;
}
if(line == null){
break;
}else{
wordsArray = line.split("\t");
}
buf.close();

PrintWriter object doesn't print all the lines in text document to out put file

public String getText()
{
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-server.xml"});
Resource resource = appContext.getResource("file:D:\\text\\test.txt");
StringBuilder builder = new StringBuilder();
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
PrintWriter out=null;
while ((line = br.readLine()) != null) {
//System.out.println(line);
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
out.println(line);
//br.close();
}
out.close();
br.close();
}catch(IOException e){
e.printStackTrace();
}
return builder.toString();
}
Create PrintWriter instance out of your while loop.
Move it outside the loop.Due to which new instance is created every line
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
You are creating new PrintWriters in the loop. Make it outside of it.
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
while ((line = br.readLine()) != null) {
//System.out.println(line);
out.println(line);
//br.close();
}
The PrintWriter is being assigned to a new instance during each loop iteration. Declare the PrintWriter outside of the loop.
String line;
PrintWriter out= new PrintWriter(new FileWriter("D:\\outputfile.txt"));
while ((line = br.readLine()) != null) {
out.println(line);
}

BufferedReader Replacement

This method is used to add words to a txt file and then sort them but I am not allowed use the Buffered Reader Function in my course. Is it possible to use Scanner to do this
public static void Option1Method() throws IOException
{
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Sure, you can. Here are equivalent codes using BufferedReader and Scanner
public void bufferedReaderVersion() throws Exception{
File f = new File("filePath");
Reader reader = new FileReader(f);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
reader.close();
}
public void scannerVersion() throws Exception{
File f = new File("filePath");
Scanner sc = new Scanner(f);
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
sc.close();
}

Categories

Resources