File tree Expand file tree Collapse file tree
src/main/java/cn/byhieg/niotutorial Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66import java .io .RandomAccessFile ;
77import java .nio .ByteBuffer ;
88import java .nio .channels .FileChannel ;
9+ import java .util .ArrayList ;
10+ import java .util .List ;
911
1012/**
1113 * Created by shiqifeng on 2017/4/10.
1416public class NioTest {
1517
1618 public static void main (String [] args ) throws IOException {
17- String s = File .separator ;
18- RandomAccessFile aFile = new RandomAccessFile ("D:" + s + "read_file.txt" ,"rw" );
19- FileChannel inChannel = aFile .getChannel ();
20-
21- ByteBuffer buf = ByteBuffer .allocate (48 );
22- int bytesRead = inChannel .read (buf );
23- while (bytesRead != -1 ) {
24- buf .flip ();
25- while (buf .hasRemaining ()) {
26- System .out .print ((char ) buf .get ());
19+ for (int i = 2 ; i <= 100 ; i ++) {
20+ if (i == 2 || i == 5 || i == 7 || i == 3 ){
21+ System .out .print (i + " " );
22+ continue ;
23+ }
24+ if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0 ) {
25+ continue ;
2726 }
2827
29- buf .clear ();
30- bytesRead = inChannel .read (buf );
28+ System .out .print (i + " " );
29+ }
30+ System .out .println ();
31+ for (int i = 2 ; i <= 100 ;i ++){
32+ if (isPrime (i )){
33+ System .out .print (i + " " );
34+ }
3135 }
36+ }
37+
38+
39+ public static boolean isPrime (int n ){
40+ if (n < 2 ){
41+ return false ;
42+ }
43+ for (int i = 2 ; i <= Math .sqrt (n );i ++){
44+ if (n % i == 0 ){
45+ return false ;
46+ }
47+ }
48+ return true ;
3249
33- aFile .close ();
3450 }
51+
52+
3553}
You can’t perform that action at this time.
0 commit comments