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 classSTEP01 { Public Static voidMain (string[] args) {Set<String>Set=NewHashset<string>(); Fileinch=NewFile ("Answer0712/aaa.txt"); File out=NewFile ("Answer0712/bbb.txt"); InputStream is=NULL; OutputStream OS=NULL; Try { is=NewFileInputStream (inch); OS=NewFileOutputStream ( out); BufferedReader BR=NewBufferedReader (NewInputStreamReader ( 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 =NewPrintWriter (NewBufferedWriter (NewOutputStreamWriter (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) I.next (); System. out . println (ss); PW.PRINTLN (ss); }
Android about java.util.NoSuchElementException error