Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2-й семестр / Примеры к лекциям / Тема 6.2 Java. Примеры.doc
Скачиваний:
64
Добавлен:
12.03.2016
Размер:
98.82 Кб
Скачать

InData.Close();

System.out.println("Чтение в измененной последовательности:");

// in = new ByteArrayInputStream(bytes);

// inData = new DataInputStream(in);

inStream = new FileInputStream(fileName);

inStream = new BufferedInputStream(inStream);

inData = new DataInputStream(inStream);

System.out.println("readInt: " + inData.readInt());

System.out.println("readDouble: " + inData.readDouble());

System.out.println("readLong: " + inData.readLong());

InData.Close();

}

catch (Exception e)

{ System.out.println("Impossible IOException occurs: " + e.toString());

e.printStackTrace();

}

}

}

Пример 4

package primer;

Import java.Io.*;

class Person implements Serializable

{ private String name; // фио

private int age; // возраст

public Person() {}

public Person(String name, int age)

{this.name = name;

this.age = age;

}

public void setage(int age){this.age = age;}

public void output()

{ System.out.printf("%-10s %d\n",name,age);

}

// запись в файл

Void diskOut(ObjectOutputStream oos)throws ioException

{ oos.writeObject(this);}

// чтение из файла

Person diskIn(ObjectInputStream ois)throws IOException, ClassNotFoundException

{ Person p =(Person)ois.readObject();

return p;

}

}

class Primer

{ public static void main(String[] args)

{ try

{ Person[] p1 = {new Person("Иванов Иван", 20),

new Person("Петров Петр", 20)};

Person[] p2 = new Person[2];

String fileName = "E:\\file3";

FileOutputStream fos = new FileOutputStream(fileName);

ObjectOutputStream oos = new ObjectOutputStream(fos);

for(int i=0; i<2;i++)

p1[i].diskOut(oos);

oos.close();

FileInputStream fis = new FileInputStream(fileName);

ObjectInputStream ois = new ObjectInputStream(fis);

int k=0;

while(fis.available() > 0)

{ p2[k]=new Person();

p2[k]=p2[k].diskIn(ois);

k++;

}

ois.close();

System.out.println("Массив p1:");

for(int i=0; i<2;i++)

p1[i].output();

System.out.println("Массив p2:");

for(int i=0; i<k;i++)

p2[i].output();

p2[1].setage(25);

System.out.println("Измененный массив p2:");

for(int i=0; i<k;i++)

p2[i].output();

System.out.println("Массив p1:");

for(int i=0; i<2;i++)

p1[i].output();

}

catch (IOException e)

{ System.out.println(e);}

catch (ClassNotFoundException e)

{ System.out.println(e);}

}

}

Пример 5

package primer;

Import java.Io.*;

class Primer

{ public static void main(String[] args)

{ String fileName = "E:\\file.txt";

//Строка, которая будет записана в файл

String data = "Строка для записи и чтения.\n";

try

{ FileWriter fw = new FileWriter(fileName);

BufferedWriter bw = new BufferedWriter(fw);

System.out.println("Write some data to file: " + fileName);

// Несколько раз записать строку

for(int i=(int)(Math.random()*10);--i>=0;)

bw.write(data);

bw.close();

// Считываем результат

FileReader fr = new FileReader(fileName);

BufferedReader br = new BufferedReader(fr);

String s = null;

int count = 0;

System.out.println("Read data from file: " + fileName);

// Считывать данные, отображая на экран

while((s=br.readLine())!=null)

System.out.println("row " + ++count + " read:" + s);

br.close();

}

catch (IOException e)

{ System.out.println(e);}

}

}

Пример 6

package primer;