It looks like you're new here. If you want to get involved, click one of these buttons!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileIO {
public static void main(String[] args) {
writeFile(\"test.txt\");
readFile(\"test.txt\");
}
/**
* reads a given textfile and prints it out
*
* @param filename
*/
public static void readFile(String filename) {
FileInputStream fin = null;
DataInputStream din = null;
try {
fin = new FileInputStream(filename);
din = new DataInputStream(fin);
BufferedReader reader = new BufferedReader(new InputStreamReader(
din));
String line;
while ((line = reader.readLine()) != null) { // read line by line
System.out.println(line); // print the content
}
} catch (FileNotFoundException e) {
System.err.println(\"File \" + filename + \" not found\");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
if (din != null) {
din.close(); // always close your streams within finally
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* writes \"Hello World\" into a textfile
*
* @param filename
*/
public static void writeFile(String filename) {
FileWriter writer = null;
BufferedWriter out = null;
try {
writer = new FileWriter(filename);
out = new BufferedWriter(writer);
out.write(\"Hello World\");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
name:Peter
age:12
public static String newline = System.getProperty(\"line.separator\");
out.write(\"name:\" + person.getName() + newline + \"age:\"
+ person.getAge());
String name = null; //initialize variables for the Person attributes
Integer age = null;
try {
fin = new FileInputStream(filename);
din = new DataInputStream(fin);
BufferedReader reader = new BufferedReader(new InputStreamReader(din));
String line;
while ((line = reader.readLine()) != null) {
String[] attributes = line.split(\":\"); //split the line
if(attributes != null && attributes.length == 2){ //make sure not to go over bounds
//there may be an empty line
if(attributes[0].equals(\"name\")){ //get name attribute
name = attributes[1];
}
if(attributes[0].equals(\"age\")){ //get age attribute
age = Integer.parseInt(attributes[1]);
}
}
}
return new Person(name, age); //return the new person object
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileIO {
public static String newline = System.getProperty(\"line.separator\");
public static void main(String[] args) {
Person peter = new Person(\"Peter\", 12);
writeFile(\"peter.txt\", peter);
Person readPerson = readFile(\"peter.txt\");
System.out.println(\"Persons name: \" + readPerson.getName());
System.out.println(\"Persons age: \" + readPerson.getAge());
}
/**
* reads a given textfile and prints it out
*
* @param filename
*/
public static Person readFile(String filename) {
FileInputStream fin = null;
DataInputStream din = null;
String name = null;
Integer age = null;
try {
fin = new FileInputStream(filename);
din = new DataInputStream(fin);
BufferedReader reader = new BufferedReader(new InputStreamReader(
din));
String line;
while ((line = reader.readLine()) != null) {
String[] attributes = line.split(\":\");
if(attributes != null && attributes.length == 2){
if(attributes[0].equals(\"name\")){
name = attributes[1];
}
if(attributes[0].equals(\"age\")){
age = Integer.parseInt(attributes[1]);
}
}
}
return new Person(name, age);
} catch (FileNotFoundException e) {
System.err.println(\"File \" + filename + \" not found\");
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e){
System.err.println(\"Value after age was no number\");
} finally {
try {
if (fin != null) {
fin.close();
}
if (din != null) {
din.close(); // always close your streams within finally
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* writes \"Hello World\" into a textfile
*
* @param filename
*/
public static void writeFile(String filename, Person person) {
FileWriter writer = null;
BufferedWriter out = null;
try {
writer = new FileWriter(filename);
out = new BufferedWriter(writer);
out.write(\"name:\" + person.getName() + newline + \"age:\"
+ person.getAge());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Person implements Serializable {import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FileIO {
public static String newline = System.getProperty(\"line.separator\");
public static void main(String[] args) {
Person peter = new Person(\"Peter\", 12);
writeFileByObjectStream(\"peter.txt\", peter);
Object obj = readFileByObjectStream(\"peter.txt\");
if (obj instanceof Person) { //do a save typecast
Person person = (Person) obj;
System.out.println(\"Persons name: \" + person.getName());
System.out.println(\"Persons age: \" + person.getAge());
}
}
public static void writeFileByObjectStream(String filename, Object object) {
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(object);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object readFileByObjectStream(String filename) {
Object obj = null;
ObjectInput in = null;
InputStream fis = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
obj = in.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.thoughtworks.xstream.XStream;
public class FileIO {
public static String newline = System.getProperty(\"line.separator\");
public static void main(String[] args) {
Person peter = new Person(\"Peter\", 12);
writeXML(\"peter.txt\", peter);
Object obj = readXML(\"peter.txt\");
if (obj instanceof Person) {
Person person = (Person) obj;
System.out.println(\"Persons name: \" + person.getName());
System.out.println(\"Persons age: \" + person.getAge());
}
}
public static void writeXML(String filename, Object object) {
XStream xstream = new XStream();
FileWriter writer = null;
try {
writer = new FileWriter(filename);
xstream.toXML(object, writer);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object readXML(String filename) {
FileReader reader = null;
Object obj = null;
try {
reader = new FileReader(filename);
XStream xstream = new XStream();
obj = xstream.fromXML(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.thoughtworks.xstream.XStream;
public class FileIO {
public static String newline = System.getProperty(\"line.separator\");
public static void main(String[] args) {
Person peter = new Person(\"Peter\", 12);
writeXML(\"peter.txt\", peter);
Object obj = readXML(\"peter.txt\");
if (obj instanceof Person) {
Person person = (Person) obj;
System.out.println(\"Persons name: \" + person.getName());
System.out.println(\"Persons age: \" + person.getAge());
}
}
public static void writeXML(String filename, Object object) {
XStream xstream = new XStream();
FileWriter writer = null;
try {
writer = new FileWriter(filename);
xstream.toXML(object, writer);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object readXML(String filename) {
FileReader reader = null;
Object obj = null;
try {
reader = new FileReader(filename);
XStream xstream = new XStream();
obj = xstream.fromXML(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
public static Person readFile(String filename) {
FileInputStream fin = null;
DataInputStream din = null;
String name = null;
Integer age = null;
try {
fin = new FileInputStream(filename);
din = new DataInputStream(fin);
BufferedReader reader = new BufferedReader(new InputStreamReader(
din));
String line;
while ((line = reader.readLine()) != null) {
String[] attributes = line.split(\":\");
if (attributes != null && attributes.length == 2) {
if (attributes[0].equals(\"name\")) {
name = attributes[1];
}
if (attributes[0].equals(\"age\")) {
age = Integer.parseInt(attributes[1]);
}
}
}
return new Person(name, age);
} catch (FileNotFoundException e) {
System.err.println(\"File \" + filename + \" not found\");
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.err.println(\"Value after age was no number\");
} finally {
try {
if (fin != null) {
fin.close();
}
if (din != null) {
din.close(); // always close your streams within finally
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void writeFile(String filename, Person person) {
FileWriter writer = null;
BufferedWriter out = null;
try {
writer = new FileWriter(filename);
out = new BufferedWriter(writer);
out.write(\"name:\" + person.getName() + newline + \"age:\"
+ person.getAge());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeFileByObjectStream(String filename, Object object) {
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(object);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object readFileByObjectStream(String filename) {
Object obj = null;
ObjectInput in = null;
InputStream fis = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
obj = in.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
}