Задача: "The Java Programming Language" Ken Arnold, James Gosling, David Holmes листинги, код, примеры из книги, исходники
Исходник: Глава 15, Ввод-Вывод (Chapter 15. Input-Output), Упражнение 15.4 (exercise) LineReader class & Test, язык: java [code #183, hits: 6300]
автор: - [добавлен: 15.09.2006]
  1. import java.io.*;
  2.  
  3. /**
  4. * Created by IntelliJ IDEA.
  5. * User: me
  6. * Date: 13.09.2006
  7. * Time: 18:07:32
  8. * To change this template use File | Settings | File Templates.
  9. */
  10. public class LineReader extends FilterReader {
  11. protected LineReader(Reader reader) {
  12. super(reader);
  13. }
  14.  
  15. public StringBuffer ReadLine() throws IOException {
  16. int c;
  17. synchronized (lock) {
  18. while ((c = read()) != -1) {
  19. if (c == Character.LINE_SEPARATOR) break;
  20. ret.append((char)c);
  21. }
  22. }
  23.  
  24. return ret;
  25. }
  26. }
  27.  
  28. // Separate Test Class
  29. public class LineReaderTest {
  30. public static void main(String[] args) throws IOException {
  31. FileInputStream fin = new FileInputStream("file1.txt");
  32.  
  33. LineReader lr = new LineReader(new InputStreamReader(fin));
  34. while ((line = lr.ReadLine()).length() != 0) {
  35. System.out.println(line);
  36. }
  37.  
  38. fin.close();
  39. }
  40. }
  41.  
Тестировалось на: java 1.5.0_04

+добавить реализацию