The program below simply reads a file called FileTest1.java(itself) and prints it to the console -
-
-
import java.util.Scanner;
-
import java.io.*;
-
class FileTest1 {
-
public static void main(String args[]) {
-
Scanner inputFile = null;
-
try {
-
inputFile = new Scanner(new File("FileTest1.java"));
-
while(inputFile.hasNext()) {
-
System.out.println(inputFile.next());
-
}
-
-
}
-
catch(FileNotFoundException fNFE) {
-
System.out.println("The file was not found");
-
}
-
finally {
-
inputFile.close();
-
}
-
-
}
-
-
}
-
-
-
Two methods of the Scanner class were used here hasNext and next.
You should be able to see what these methods are doing here if not then you can check with the Scanner page on sun's Java API.
This is the recommended way of reading a file these days.
To write to a file, use the FileWriter class wrapped in a BufferedWriter -
-
-
public static void writeFile(String fileName) {
-
BufferedWriter br = null;
-
try {
-
br = new BufferedWriter(new FileWriter(fileName));
-
String[] semiFinals = {"Australia", "West Indies", "South Africa", "Sri Lanka"};
-
for(String s : semiFinals) {
-
br.write(s);
-
br.write(System.getProperty("line.separator"));
-
}
-
br.close();
-
}
-
catch(IOException iO) {
-
System.out.println("The file could not be created/opened/closed");
-
}
-
}
-
-
Notice the use of System.getProperty("line.separator"); to write a return
This method of opening a file overrides the data that was on the file.
To open the file in append mode, simply use - br = new BufferedWriter(new FileWriter(fileName, true));
-
These approaches should not be used for manipulating .doc, pdf, xls e.t.c Instead one should use more specific packages
19 16362
I don't want to exhibit myself as a nitpicker but why don't you just simply use
a BufferedReader for this instead of a Scanner. BufferedReaders are available
in Java 1.4 and before while those Scanners are only available starting from
Java 1.5 and up. BufferedReaders are much simpler too.
kind regards,
Jos (<--- nitpicker ;-)
I don't want to exhibit myself as a nitpicker but why don't you just simply use
a BufferedReader for this instead of a Scanner. BufferedReaders are available
in Java 1.4 and before while those Scanners are only available starting from
Java 1.5 and up. BufferedReaders are much simpler too.
kind regards,
Jos (<--- nitpicker ;-)
And for those who prefer the nitpicker's advice -
-
import java.io.*;
-
class FileTest1 {
-
public static void main(String args[]) {
-
BufferedReader inputFile = null;
-
try {
-
inputFile = new BufferedReader (new FileReader("FileTest1.java"));
-
while((String line = inputFile.readLine()) != null) {
-
System.out.println(line);
-
}
-
-
}
-
catch(FileNotFoundException fNFE) {
-
System.out.println("The file was not found");
-
}
-
catch(IOException iO) {
-
System.out.println("IO error");
-
}
-
finally {
-
inputFile.close();
-
}
-
-
}
-
-
}
-
-
And for those who prefer the nitpicker's advice
:-)
kind regards,
Jos
ps. while I'm at it, other IOExceptions can be thrown too you know ;-)
:-)
kind regards,
Jos
ps. while I'm at it, other IOExceptions can be thrown too you know ;-)
But of course Jos.
At which point I will now have to edit the post.
But of course Jos.
At which point I will now have to edit the post.
I'm sorry for stirring this all up; I'm a nasty nitpicker ;-)
kind regards,
Jos
hello every body
I need a help please help me if you can what functions can we use to send multimedia (vedio,oudio,imeges) using java through socket . send any thing about this subject to my e-mail : <email removed:Against site rules>
thanks ................
hello every body
I need a help please help me if you can what functions can we use to send multimedia (vedio,oudio,imeges) using java through socket . send any thing about this subject to my e-mail : dreams294@hotmail.com
thanks ................
Please start you own topic for your question; don't pollute anyone else's topic.
kind regards,
Jos
I don't want to exhibit myself as a nitpicker but why don't you just simply use
a BufferedReader for this instead of a Scanner.
Can't the Scanner class be a better option if, for instance, someone wanted to easily extract various forms of data from the input say one structured with integers floats and strings all on the same line? Of course you CAN use the BufferedReader, but the scanner may be a better choice for an actual solution to a problem.
Just my 2c.
Can't the Scanner class be a better option if, for instance, someone wanted to easily extract various forms of data from the input say one structured with integers floats and strings all on the same line? Of course you CAN use the BufferedReader, but the scanner may be a better choice for an actual solution to a problem.
Just my 2c.
Sure, a Scanner would be much better for that purpose, but for the purpose
shown in the article a Scanner would be overkill; that's why I was nitpicking ;-)
(and besides that, Scanners are only here with us, starting at Java 1.5)
kind regards,
Jos
Sure, a Scanner would be much better for that purpose, but for the purpose
shown in the article a Scanner would be overkill; that's why I was nitpicking ;-)
(and besides that, Scanners are only here with us, starting at Java 1.5)
kind regards,
Jos
I want to read on a field by field basis say
Name Age DateOfBirth
Jyo 25 28-11-82
Ram 30 20-12-81
say I want to read the above file and find out the entries whose DOB is between two given dates
I want to read on a field by field basis say
Name Age DateOfBirth
Jyo 25 28-11-82
Ram 30 20-12-81
say I want to read the above file and find out the entries whose DOB is between two given dates
Read the API docs for the Scanner class and use the code supplied
in this tip of the week to read all non-white space tokens. Oh, before I forget,
the FileReader or FileInputStream classes top it all off.
kind regards,
Jos
Hey a great posting would be how to use a Arraylist.
code to build arraylist
code to create output file and write arraylist
code to read input files and rebuild an Arraylist
code to display the contents on an arraylist.
This was a hard one for me to understand, and sometimes it still is.
nomad.
just my two cents.
Hey a great posting would be how to use a Arraylist.
code to build arraylist
code to create output file and write arraylist
code to read input files and rebuild an Arraylist
code to display the contents on an arraylist.
This was a hard one for me to understand, and sometimes it still is.
Why don't you try it yourself? You try to write a nice concise article; post your
attempts in this forum and I for one will help you out when you're stuck. When
the article is finished you can stick it in the Articles section. deal?
kind regards,
Jos
Why don't you try it yourself? You try to write a nice concise article; post your
attempts in this forum and I for one will help you out when you're stuck. When
the article is finished you can stick it in the Articles section. deal?
kind regards,
Jos
How did I get invloved with this. Heck I'm just starting out in Java. I have only done a several projects. Two with Arraylist and I had a very very hard time doing those....I don't even know if I did those right, because the instructor has not grade them.
Besides I think the viewer would rather see ane Expert like you do it Jos.
nomad.
Maybe in a few months when I get more under my belt. I would do one for Flash or HTML...
How did I get invloved with this. Heck I'm just starting out in Java. I have only done a several projects. Two with Arraylist and I had a very very hard time doing those....I don't even know if I did those right, because the instructor has not grade them.
Besides I think the viewer would rather see ane Expert like you do it Jos.
Stage fright? ;-) I promised I'd help you out and maybe during the 'making' of
the article others will join us in this forum. It could be fun. I promise I won't
'attack' you ;-) Why not give it a try? All we have to come up with now is a
nice, simple problem/project that naturally has to do with Lists.
kind regards,
Jos
Stage fright? ;-) I promised I'd help you out and maybe during the 'making' of
the article others will join us in this forum. It could be fun. I promise I won't
'attack' you ;-) Why not give it a try? All we have to come up with now is a
nice, simple problem/project that naturally has to do with Lists.
kind regards,
Jos
OK Jos...
I making a arraylist program for you It might be too long but it is one of my assignments. I could shorten it.
Anyways here is the code so far, but I'm having problem
I can not invoke the findPerson method. It should work because I have done this in the past.
I did a debug and got this error.
<terminated>EmployeeData (1) [Java Application]
<disconnected>arrayproject.EmployeeData at localhost:1253
<terminated, exit value: 0>C:\Program Files\EasyEclipse Desktop Java 1.2.1\jre\bin\javaw.exe (May 9, 2007 2:04:22 PM)
I have attached my whole project and if someone can help me that would be great.. - class PersonClass {
-
private String empid;
-
private String lname;
-
private String fname;
-
private String street;
-
private String city;
-
private String state;
-
private String zip;
-
private double payrate;
-
private int yearsworked;
-
public PersonClass(String id) {
-
empid = id;
-
}
-
public PersonClass(String id, String ln, String fn, String st, String ct, String se, String zp, double pr, int yw) {
-
empid = id;
-
lname = ln;
-
fname = fn;
-
street = st;
-
city = ct;
-
state = se;
-
zip = zp;
-
payrate = pr;
-
yearsworked = yw;
-
-
}
-
-
// accessors
-
public String getID() {return empid;}
-
-
public String getFname() {return fname;}
-
-
public String getLname() {return lname;}
-
-
public String getStree() {return street;}
-
-
-
public String getCity() {return city;}
-
-
public String getState() {return state;}
-
-
public String getZip() {return zip;}
-
-
public double getPayrate() {return payrate;}
-
-
public int getYearsworked() {return yearsworked;}
-
-
-
}
-
-
-
public class EmployeeData {
-
static ArrayList<PersonClass> arlist;
-
static Scanner kbd;
-
-
public static PersonClass makePerson() {
-
PersonClass temp = null;
-
-
// prompt for data
-
String id;
-
String ln;
-
String fn;
-
String st;
-
String se;
-
String ct;
-
String zp;
-
double pr;
-
int years;
-
-
System.out.print("Enter ID Number ==>");
-
id = kbd.next();
-
-
System.out.print("Enter Last Name ==>");
-
ln = kbd.next();
-
-
System.out.print("Enter First Name ==>");
-
fn = kbd.next();
-
-
System.out.print("Enter the address==>");
-
st = kbd.next();
-
-
System.out.print("Enter City ==>");
-
ct = kbd.next();
-
-
System.out.print("Enter State ==>");
-
se = kbd.next();
-
-
System.out.print("Enter Zip ==>");
-
zp = kbd.next();
-
-
System.out.print("Enter payrate as double ==>");
-
pr = kbd.nextDouble();
-
-
System.out.print("Enter years worked ==>");
-
years = kbd.nextInt();
-
-
// make an object
-
temp = new PersonClass(id, ln,fn,st,ct,se, zp, pr,years);
-
-
return temp;
-
}
-
-
public void displayMatch() {
-
-
String id_flag = "";
-
Scanner kbd = new Scanner(System.in);
-
System.out.println("Enter your info please ie: AB1234: ");
-
id_flag = kbd.next();
-
boolean notfound = true;
-
for (PersonClass e : arlist) {
-
String emp = e.getID();
-
if (emp.equals(id_flag)) {
-
System.out.println("Hello" + ("e.lname"));
-
notfound = false;
-
}
-
}
-
if (notfound == true) {
-
System.out.println("Error - Employee not found");
-
// back to menu?
-
}
-
-
}//close findperson
-
-
public static void main(String[] args) {
-
EmployeeData emp = new EmployeeData();
-
// make array list object
-
arlist = new ArrayList<PersonClass>();
-
-
// make a scanner
-
kbd = new Scanner(System.in);
-
-
int choice;
-
System.out.println("Make a Section: ");
-
System.out.println("1. Enter Employees ");
-
System.out.println("2. Find Employees ");
-
System.out.println("3. Exit this Program ");
-
System.out.print("\nPlease press Enter afer each response");
-
System.out.println("Enter your chose please: ");
-
choice = kbd.nextInt();
-
kbd.nextLine();
-
if (choice == 1) { // if 1 is select go to makePerson
-
-
// create people until select stop
-
boolean endData = false;
-
-
while (!endData) {
-
PersonClass temp = makePerson();
-
arlist.add(temp);
-
System.out.println("Add More employees (Y/N)-->");
-
-
String ans = kbd.next();
-
-
if (ans.equalsIgnoreCase("N")) {
-
endData = true;
-
}
-
}//close while loop
-
if (choice == 2) { // if 2 is select go to find
-
emp.displayMatch();
-
-
-
}// close the choice==2
-
if (choice == 3) {
-
System.out.printf("Good bye");
-
}// close the choice == 3
-
-
-
// print out all elements of array list
-
for (PersonClass idx : arlist) {
-
System.out.printf("Employee Id is %n", idx.getID());
-
System.out.printf("Name is %s - %s%n", idx.getFname(),idx.getLname());
-
System.out.printf("Street is %s%n", idx.getStree());
-
System.out.printf("City is %s%n", idx.getCity());
-
System.out.printf("State is %s%n", idx.getState());
-
System.out.printf("Zip Code is %s%n", idx.getZip());
-
System.out.printf("Payrate is %8.2f%n", idx.getPayrate());
-
System.out.printf("Years worked are %d\n", idx.getYearsworked());
-
System.out.println("--------------------");
-
}
-
}
-
}
-
}
Thanks a bunch
nomad
I created a new thread for Nomad's Article under construction so there'll be no
need anymore to hijack this thread.
kind regards,
Jos
The program below simply reads a file called FileTest1.java(itself) and prints it to the console -
-
-
import java.util.Scanner;
-
import java.io.*;
-
class FileTest1 {
-
public static void main(String args[]) {
-
Scanner inputFile = null;
-
try {
-
inputFile = new Scanner(new File("FileTest1.java"));
-
while(inputFile.hasNext()) {
-
System.out.println(inputFile.next());
-
}
-
-
}
-
catch(FileNotFoundException fNFE) {
-
System.out.println("The file was not found");
-
}
-
finally {
-
inputFile.close();
-
}
-
-
}
-
-
}
-
-
-
Two methods of the Scanner class were used here hasNext and next.
You should be able to see what these methods are doing here if not then you can check with the Scanner page on sun's Java API.
This is the recommended way of reading a file these days.
To write to a file, use the FileWriter class wrapped in a BufferedWriter -
-
-
public static void writeFile(String fileName) {
-
BufferedWriter br = null;
-
try {
-
br = new BufferedWriter(new FileWriter(fileName));
-
String[] semiFinals = {"Australia", "West Indies", "South Africa", "Sri Lanka"};
-
for(String s : semiFinals) {
-
br.write(s);
-
br.write(System.getProperty("line.separator"));
-
}
-
br.close();
-
}
-
catch(IOException iO) {
-
System.out.println("The file could not be created/opened/closed");
-
}
-
}
-
-
Notice the use of System.getProperty("line.separator"); to write a return
This method of opening a file overrides the data that was on the file.
To open the file in append mode, simply use - br = new BufferedWriter(new FileWriter(fileName, true));
-
These approaches should not be used for manipulating .doc, pdf, xls e.t.c Instead one should use more specific packages
how to apply in JFileChooser?..that it can read the file that was selected.
Usually loading a whole file at once is faster than looping through it line by line. Especially if the file resides on a network device.
Here is the code: -
// check existence
-
File file = new File(sourceFileName);
-
if (! file.exists()) throw new IOException("ERROR: no such source file: " + sourceFileName);
-
-
// read as string
-
FileInputStream fileInputStream = new FileInputStream(sourceFileName);
-
byte data[] = new byte[fileInputStream.available()];
-
fileInputStream.read(data);
-
String content = new String(data);
-
-
// do whatever you want with the result
-
System.out.println("File contains:" + content);
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by dunnm |
last post: by
|
5 posts
views
Thread by Benjamin de Waal |
last post: by
|
2 posts
views
Thread by John Salerno |
last post: by
|
2 posts
views
Thread by Robert Reijntjes |
last post: by
|
8 posts
views
Thread by smeenehan |
last post: by
|
6 posts
views
Thread by arne.muller |
last post: by
|
10 posts
views
Thread by Tyler |
last post: by
|
7 posts
views
Thread by random guy |
last post: by
|
2 posts
views
Thread by Clive Green |
last post: by
| | | | | | | | | | | |