Image:LineIterator.java

From Carboogle

LineIterator.java (4KB, MIME type: unknown/unknown)

Warning: This file may contain malicious code, by executing it your system may be compromised.

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.text.DecimalFormat;
  8. import java.text.NumberFormat;
  9. import java.util.Iterator;
  10. import java.util.NoSuchElementException;
  11.  
  12. /**
  13. * Iterator over each line of a resource : file, url, ...
  14. */
  15. public final class LineIterator implements Iterator
  16. {
  17. private final BufferedReader in;
  18. private String next;
  19. /**
  20. * Create a new line iterator on a resource
  21. * @param resource The resource
  22. * @throws IOException The iterator could not be created
  23. */
  24. public LineIterator(final URL resource) throws IOException
  25. {
  26. super();
  27. in = new BufferedReader(new InputStreamReader(resource.openStream()));
  28. next = in.readLine();
  29. }
  30.  
  31. /**
  32. * @see java.util.Iterator#hasNext()
  33. */
  34. public boolean hasNext()
  35. {
  36. return next != null;
  37. }
  38.  
  39. /**
  40. * @see java.util.Iterator#next()
  41. */
  42. public Object next()
  43. {
  44. // If there is no next element we throw an exception as stated in the interface
  45. if (next == null)
  46. {
  47. }
  48. // prefetch next line for optimization
  49. final String current = next;
  50. try
  51. {
  52. next = in.readLine();
  53. }
  54. catch (final IOException e)
  55. {
  56. next = null;
  57. }
  58. // If an error occured or if the stream has no new line
  59. if (next == null)
  60. {
  61. try
  62. {
  63. in.close();
  64. }
  65. catch (IOException e1)
  66. {
  67. // nothing to do
  68. }
  69. }
  70. return current;
  71. }
  72.  
  73. /**
  74. * @see java.util.Iterator#remove()
  75. */
  76. public void remove()
  77. {
  78. }
  79. /**
  80. * TEST METHOD
  81. * @param args args
  82. */
  83. public static void main(String args[]) throws MalformedURLException, IOException
  84. {
  85. final NumberFormat nf = new DecimalFormat();
  86. int count;
  87. nf.setMinimumIntegerDigits(4);
  88. nf.setGroupingUsed(false);
  89. nf.setParseIntegerOnly(true);
  90. // Example to retreive each lines of a caracter file :
  91. count = 0;
  92. final URL resource1 = new File("./src/LineIterator.java").toURL();
  93. for(final Iterator i = new LineIterator(resource1); i.hasNext();)
  94. {
  95. System.out.println("Line " + nf.format(++count) + ": " + i.next());
  96. }
  97. // Example to retreive each lines of a web resource :
  98. count = 0;
  99. final URL resource2 = new URL("http://www.labunix.uqam.ca/~charki/INF4482/index.html");
  100. for(final Iterator i = new LineIterator(resource2); i.hasNext();)
  101. {
  102. System.out.println("Line " + nf.format(++count) + ": " + i.next());
  103. }
  104. }
  105. }

File history

Legend: (cur) = this is the current file, (del) = delete this old version, (rev) = revert to this old version.
Click on date to see the file uploaded on that date.


The following pages link to this file:

Mycila.com

Mycila projects

Other projects

ports

articles

lessons