Reading Files and Print

Hey Guys,

I'm a beginner to java and I wanna write this program. I wanna create files including information of the students in my class. Each file contains a student's name, e-mail, school, hobby. There are almost 30 files.

When I want to find students whose school is "School A" , what I want to do is, I want my program to read all the files and find the Particular name "School A" and print all contents in the file(s).

I give a the user to input. So that user may input any name or a phrace, after that what my program is gonna do is read all the file and if it finds any content that is equal to the user input its gonna print the entire file content.

In a case where more than one student are from the same school it should print all the information about all those students. I think I'm clear.. Please help me with this..
View Answers

April 29, 2010 at 10:54 AM

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;

public class Files{
public static void main(String[]args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("Enter school Name");
String sch = input.nextLine();
String fname[]={"s1.txt","s2.txt","s3.txt","s4.txt"};
File file = new File("data.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);

for (int i=0;i<fname.length;i++){
File f = new File(fname[i]);
try {
BufferedReader freader = new BufferedReader(new FileReader(f));
String s;
while ((s = freader.readLine()) != null) {
String[] st = s.split(" ");
String name = st[0];
String email = st[1];
String school = st[2];
String hobby = st[3];
if (school.equals(sch)) {
System.out.println(name+" "+email+" "+hobby);
out.write(name+" "+email+" "+school+" "+hobby);
out.newLine();
}
}
freader.close();
}
catch (Exception e) { }
}
out.close();

}
}

In the above code, we have read only four files.Similarly,you can read all the files.

Thanks









Related Tutorials/Questions & Answers:
Reading Files and Print - Java Beginners
Reading Files and Print  Hey Guys, I'm a beginner to java and I wanna write this program. I wanna create files including information.... There are almost 30 files. When I want to find students whose school is "School
Reading and writting multiple files
Reading and writting multiple files  how can i read and write say two different files at the same time using threads
Advertisements
Reading multiple xml files
Reading multiple xml files  How can we read many xml files from a folder? The same procedure as that of "listfiles
Reading files in Java
Reading files in Java  I have to make a program in my project.... Please provide me the example code for reading big text file in Java. What do you suggest for Reading files in Java? Thanks
Reading and Writing files - Java Beginners
Reading and Writing files  Hello, please help me to Develop a simple Java application that, when run, Welcomes the users and tells them the name... to investigate writing to files a little (can be also done using Properties
How to print differences of two text files
How to print differences of two text files  I have the code to compare 2 different text files, however How can I put the difference of 2 text files in other file or print the difrference in the console?? Thanks
How to print list of files in client system
How to print list of files in client system  have some list of files in server.i just want to print that files in client machine with out opening the file.i give the option for user like printall.if user click printall prints
Reading from remote files using PHP,Ubuntu and Windows machine.
Reading from remote files using PHP,Ubuntu and Windows machine.  Hi, Great work you doing here and I totally love the look and feel of the new site... on a windows machine within the same network. After reading from it, the script
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... this: /*this program reads multiple files * from a single directory. */ package elite.tech.com... File(inpath); File files[]; files = Folder.listFiles
print
print  How to print JFrame All Componant?   Please visit the following link: http://www.roseindia.net/java/example/java/swing/Print.shtml
Print
into the stream and, as opposed to print() method, gets you to the new line after the text
files
/core/files/storeobjectsinFile.html
files
files  write a java program to calculate the time taken to read a given number of files. file names should be given at command line.   Hello Friend, Try the following code:ADS_TO_REPLACE_1 import java.io.*; import
svg file reading and display
svg file reading and display  i want a sample program for reading and displaying the content of a svg file
ModuleNotFoundError: No module named 'print_print'
ModuleNotFoundError: No module named 'print_print'  Hi, My Python... 'print_print' How to remove the ModuleNotFoundError: No module named 'print_print' error? Thanks   Hi, In your python environment
Reading a text file in java
Reading a text file in java  What is the code for Reading a text file... in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks
Video Tutorial of reading file in Java
tutorials of reading files in Java How To Read... programming language comes with rich set of API for accessing the files of various... text file and print on the console.ADS_TO_REPLACE_1 Then you will learn how
print a form of *
print a form of *   * *** ***** ******* ********* ******* ***** *** *   Post the format properly
Print Button
Print Button  i want the source code for printing details of jform.. so plz help me for source code of print button
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
print the newline
print the newline  Why doesn?t the following code print the newline properly? <?php $str = ?Hello, there.\nHow are you?\nThanks for visiting.?; print $str; ?>   Hi friends, Inside the single quotes \n (newline
print numbers
print numbers  1234 123 12 1234   1234 123 12 1234
Print the document
Print the document  I tried to print web page.I am using the below...() { var printWindow = window.open('test', 'Print Window', 'height=600,width...;Print Window</title>'); printWindow.document.write('</head>
Print the document
Print the document  I tried to print web page.I am using the below...() { var printWindow = window.open('test', 'Print Window', 'height=600,width...;Print Window</title>'); printWindow.document.write('</head>
Print a statement
Print a statement  hello what would we output for this statement System.out.println ("5"+"A" + 3);   helloADS_TO_REPLACE_1 output will be 5A3
date print
date print  how can i print the date in jsp page in the following formate month-date-year. (example. march 8 2012
date print
date print  how can i print the date in jsp page in the following formate month-date-year. (example. march 8 2012
print the sequence
print the sequence  how can I write the code by using for loop to produce this following statement: 10,9,8,7,6,5,4,3,2,1,buzz! please, help me
Reading big file in Java
Reading big file in Java  How to read a big text file in Java program?   Hi, Read the complete tutorial at How to read big file line by line in java? Thanks
Problem reading word file
Problem reading word file  Deepak you provide me code for extarcting equation from a word file and also to write in a word file.But when I again want to read preveously created word file(created by your code) it gives an error
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
Reading Value From console
Reading Value From console  In case of String data Type readLine method of DataInputStream class read complete line of the given string but the next method of Scanner class doesn't read the complete line of String. why
Reading Value From console
Reading Value From console  In case of String data Type readLine method of DataInputStream class read complete line of the given string but the next method of Scanner class doesn't read the complete line of String. why
diamond print
diamond print   print the pattern 1 2 3 4 5 6 7 8 9 10 11 12
print initials
print initials  How to print initials of a name without using arrays?   Hi Friend, You can try the following code:ADS_TO_REPLACE_1 import java.util.*; public class InitialName { public static void main(String
Reading Text file and storing in map
Reading Text file and storing in map  Hi I have multiple text files. I want to read thoses files and store those records in map. Map will be of "LinkedHashMap<String, Map<String, String>>" type. Please let me know
Best way to reading file in java
Best way to reading file in java  Hi, As a beginner I want to learn about Java programming and make a program for reading a text file in java. What is the best way for reading file in Java? Thanks
Reading cookies in jsp
Reading cookies in jsp   How read cookies in jsp ?   Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can
web page reading in java
web page reading in java  i wanna read webpage in that i want to get the data from the particular tags like (,) and store into the data base...... can any body tell me the solution? plz urgnt. thanks and regards, jasti  
Reading file into bytearrayoutputstream
input stream and byte output stream. This is and good example of reading file... In this example we have used the class InputStream for reading the file... import java.io.*; /** * Example of Reading file into byte array
Reading excel via JSP
Reading excel via JSP   The problem is while reading cells values... the space between words and get full cell value? I am reading the value...(means while reading the cell values space should be ignored between the words
Reading a big file effeciently
Reading a big file effeciently  How to read a large text file quickly without memory error in Java? What is the best method to read a big file very efficiently? Thanks   Hi, Kindly check the program Java Read File
To print initials
To print initials  import java.util.Scanner; class initials { String a ; int e ; char f ; int b ; int c ; char d; Scanner sc = new Scanner(System.in); void main
Print command
Print command  Can I use System.out.println command in Struts form bean or Struts action class. I am using Struts 1.3.8 but when I write this command. It does not work. Please reply me as soon as possible. Thanks.   
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed...: http://www.roseindia.net/tutorial/java/core/files/insertfiledataToJTable.html
Reading duplicate keys from .properties file - Development process
Reading duplicate keys from .properties file  Hi, I am reading a .properties file using a basic java program.Though the .properties file... the duplicate keys and print it on console. It is urgent requirement,please reply
ModuleNotFoundError: No module named 'reading-helper'
ModuleNotFoundError: No module named 'reading-helper'  Hi, My... named 'reading-helper' How to remove the ModuleNotFoundError: No module named 'reading-helper' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'reading-image'
ModuleNotFoundError: No module named 'reading-image'  Hi, My... 'reading-image' How to remove the ModuleNotFoundError: No module named 'reading-image' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'smart-reading'
ModuleNotFoundError: No module named 'smart-reading'  Hi, My... 'smart-reading' How to remove the ModuleNotFoundError: No module named 'smart-reading' error? Thanks   Hi, In your python