Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Reading Files and Print
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Samantha
Reading Files and Print
1 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners


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..

Advertisement
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 multiple xml files
Reading multiple xml files  How can we read many xml files from a folder? The same procedure as that of "listfiles
Advertisements
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
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 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
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
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: import java.io.*; import java.util.*; class
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
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
print a form of *
print a form of *   * *** ***** ******* ********* ******* ***** *** *   Post the format properly
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
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 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
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 a statement
Print a statement  hello what would we output for this statement System.out.println ("5"+"A" + 3);   hello output will be 5A3
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
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: import java.util.*; public class InitialName { public static void main(String[] args
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.   
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
Advertisements
 

 

 

DMCA.com