Reprinted from http://blog.csdn.net/ppact/article/details/1688445
Wrote an example of copying a file from A to B, which requires that duplicate rows be removed.
So think of set, which would have been very easy, and the result of writing data out of the wrong
Java.util.NoSuchElementException
On the network repeatedly check also no related content, so his slowly find, finally found. Below to write the example of this error and analysis, I hope to timely help and I also made a small mistake rookie!
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;
public class Step01 {
public static void Main (string[] args) {
set<string> set = new hashset<string> ();
File in = new file ("Answer0712/aaa.txt");
File out = new file ("Answer0712/bbb.txt");
InputStream is = null;
OutputStream OS = null;
try {
is = new FileInputStream (in);
OS = new FileOutputStream (out);
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
while (true) {
String str = br.readline ();
if (str = = null)
Break
Set.add (str);
}
Iterator i = Set.iterator ();
while (I.hasnext ()) {
SYSTEM.OUT.PRINTLN ("Null value judgment:" +i.next ());
// }
PrintWriter pw = new PrintWriter (New BufferedWriter (
New OutputStreamWriter (OS)), true);
Iterator i = Set.iterator ();
while (I.hasnext ()) {
System.out.println (I.next ()); This sentence is the culprit .
Pw.println (I.next ());
}
Pw.flush ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
Is.close ();
Os.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
The article marked the yellow one, originally to print out, in the console to see if the data written is correct.
The result is a java.util.NoSuchElementException error, that is, the yellow one, and one more iterator next ().
So it's wrong. It would be better to remove it, or instead
while (I.hasnext ()) {
string ss = (string) i.next ();
SYSTEM.OUT.PRINTLN (ss);
PW.PRINTLN (ss);
can solve the problem.
An example of a java.util.NoSuchElementException error