.


:




:

































 

 

 

 


RandomAccessFile(String name, String mode);




RandomAccessFile(File file, String mode);

mode "r" "rw" .

/* # 4: : RandomFiles.java */

package chapt09;

import java.io.*;

 

public class RandomFiles {

public static void main(String[] args) {

double data[] = { 1, 10, 50, 200, 5000 };

try {

RandomAccessFile rf =

new RandomAccessFile("temp.txt", "rw");

for (double d: data)

rf.writeDouble(d); //

/* */

for (int i = data.length - 1; i >= 0; i--) {

rf.seek(i * 8);

// double 8-

System. out. println(rf.readDouble());

}

rf.close();

} catch (IOException e) {

System.err.println(e);

}

}

}

:

5000.0

200.0

50.0

10.0

1.0

/ Java , . System java.lang in, InputStream, out, err PrintStream, public static , . , .

PrintWriter, Writer.

( ) PrintWriter:

New PrintWriter(new BufferedWriter(

new FileWriter(new File("file.txt"))));

BufferedWriter - FileWriter, BufferedReader FileReader.

.

// # 5: : DemoWriter.java

package chapt09;

import java.io.*;

 

public class DemoWriter {

public static void main(String[] args) {

File f = new File("res.txt");

FileWriter fw = null;

try {

fw = new FileWriter(f, true);

} catch (IOException e) {

System. err. println(" " + e);

System. exit (1);

}

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bw);

 

double [] v = { 1.10, 1.2, 1.401, 5.01 };

for (double version: v)

pw.printf("Java %.2g%n", version);

pw.close();

}

}

res.txt :

Java 1.1

Java 1.2

Java 1.4

Java 5.0

PrintWriter printf(). BufferedWriter FileWriter println(), print(), printf(), format(), write(), append().

Java 1.1 Java 1.2 , . BufferedReader Reader read() readLine() . FileReader :

new BufferedReader(new FileReader(new File("f.txt")));

:

// # 6: : DemoReader.java

package chapt09;

import java.io.*;

 

public class DemoReader {

public static void main(String[] args) {

try {

BufferedReader br =

new BufferedReader(new FileReader("res.txt"));

String tmp = "";

while ((tmp = br.readLine())!= null) {

//

String[] s = tmp.split("\\s");

//

for (String res: s)

System. out. println(res);

}

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

:

Java

1.1

Java

1.2

Java

1.4

Java

5.0

, .

. . .

, Serializable. . . , , static transient. transient static , , , , . , transient , ( null), static , , .

Serializable , , . . ObjectOutputStream. writeObject(Object ob) ob . ObjectInputStream readObject(), Object. .

, Serializable : , . .

/* # 7: : Student.java: DemoSerialization.java */

package chapt09;

import java.io.*;

 

class Student implements Serializable{

protected static String faculty;

private String name;

private int id;

private transient String password;

private static final long serialVersionUID = 1L;

/* */

public Student(String nameOfFaculty, String name,

int id, String password){

faculty = nameOfFaculty;

this. name = name;

this. id = id;

this. password = password;

}

public String toString(){

return "\nfaculty " + faculty + "\nname " + name

+ "\nID " + id + "\npassword " + password;

}

}

 

public class DemoSerialization {

public static void main(String[] args) {

//

Student goncharenko =

new Student("MMF", "Goncharenko", 1, "G017s9");

System. out. println(goncharenko);

File fw = new File("demo.dat");

try {

ObjectOutputStream ostream =

new ObjectOutputStream(

new FileOutputStream(fw));

 

ostream.writeObject(goncharenko);

ostream.close();

} catch (IOException e) {

System. err. println(e);

}

Student.faculty="GEO"; // static-

//

File fr = new File("demo.dat");

try {

ObjectInputStream istream =

new ObjectInputStream(

new FileInputStream(fr));

 

Student unknown =

(Student)istream.readObject();

istream.close();

System. out. println(unknown);

} catch (ClassNotFoundException ce) {

System. err. println(ce);

System. err. println(" ");

} catch (FileNotFoundException fe) {

System. err. println(fe);

System. err. println(" ");

} catch (IOException ioe) {

System. err. println(ioe);

System. err. println(" ");

}

}

}

:

Faculty MMF

Name Goncharenko

ID 1

Password G017s9

Faculty GEO

Name Goncharenko

ID 1

Password null

name id unknown , . e passwrd transient , ( null). faculty, , , , goncharenko MMF, GEO. , .

, , Serializable.

, Serializable, . . , , Serializable, private static final long serialVersionUID. . , , , .

. , static - .

. , java.io.InvalidClassException. , .

, . , .
private static final long serialVersionUID.

Serializable Externalizable, :





:


: 2016-04-03; !; : 462 |


:

:

, .
==> ...

1332 - | 1255 -


© 2015-2024 lektsii.org - -

: 0.032 .