记录⼀个程序的使⽤次数,超过5次提示去注册
import java. io. * ;
import java. util. * ;
public class Work1 {
public static void main ( String [ ] args) throws IOException {
File file = new File ( "zz.exe" ) ;
if ( ! file. exists ( ) )
file. createNewFile ( ) ;
FileInputStream inputStream = new FileInputStream ( file) ;
Properties properties = new Properties ( ) ;
properties. load ( inputStream) ;
int count = 1 ;
String value = properties. getProperty ( "time" ) ;
if ( value != null )
count = Integer . parseInt ( value) ;
if ( count > 5 ) {
System . out. println ( "请注册" ) ;
}
count++ ;
properties. setProperty ( "time" , count+ "" ) ;
FileOutputStream outputStream = new FileOutputStream ( file) ;
properties. store ( outputStream, "haha" ) ;
inputStream. close ( ) ;
outputStream. close ( ) ;
}
}
有五个学⽣,每个学⽣有3⻔课的成绩, 从键盘输⼊以上数据(包括姓名,三⻔课成绩), 输⼊的格式:如:zhagnsan,30,40,60计算出总成绩, 并把学⽣的信息和计算出的总分数⾼低顺序存放在磁盘⽂件"stud.txt"中。
import java. io. * ;
import java. util. * ;
public class Work2 {
public static void main ( String [ ] args) throws IOException {
File file = new File ( "Bigdataday20\\src\\file\\stud.txt" ) ;
if ( ! file. exists ( ) )
file. createNewFile ( ) ;
Compare bj = new Compare ( ) ;
TreeSet < String > ts = new TreeSet < > ( bj) ;
BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in) ) ;
for ( int i = 1 ; i <= 5 ; i++ ) {
String line = null ;
System . out. println ( "请输⼊第" + i + "个学⽣的姓名和三⻔课的成绩" ) ;
line = br. readLine ( ) ;
String ar = line;
String arr[ ] = new String [ 4 ] ;
arr = ar. split ( "," ) ;
int sum = Integer . parseInt ( arr[ 1 ] ) + Integer . parseInt ( arr[ 2 ] ) + Integer . parseInt ( arr[ 3 ] ) ;
ts. add ( arr[ 0 ] + "," + arr[ 1 ] + "," + arr[ 2 ] + "," + arr[ 3 ] + "," + sum + "" ) ;
}
br. close ( ) ;
FileWriter fw = new FileWriter ( file) ;
Iterator < String > ite = ts. iterator ( ) ;
while ( ite. hasNext ( ) ) {
String ss = ite. next ( ) ;
fw. write ( ss) ;
fw. write ( "\r\n" ) ;
fw. flush ( ) ;
}
fw. close ( ) ;
}
}
class Compare implements Comparator < String > {
public int compare ( String s1, String s2) {
String ss1[ ] = new String [ 5 ] ;
String ss2[ ] = new String [ 5 ] ;
ss1 = s1. split ( "," ) ;
ss2 = s2. split ( "," ) ;
return Integer . parseInt ( ss1[ 4 ] ) - Integer . parseInt ( ss2[ 4 ] ) ;
}
}
【难】设计⼀个⽅法,计算⼀个⽂件夹的总⼤⼩(由所有的⼦⽂件、⼦⽂件夹中的⼦⽂件夹⼤⼩组成)
public class Demo1 {
public static void main ( String [ ] args) {
long size = calculate ( new File ( "Bigdataday20\\src\\file" ) ) ;
System . out. println ( size) ;
}
private static long calculate ( File file) {
if ( file. isDirectory ( ) ) {
long length = 0 ;
File [ ] files = file. listFiles ( ) ;
for ( File file1 : files) {
length += calculate ( file1) ;
}
return length;
} else {
return file. length ( ) ;
}
}
}
【简】在桌⾯上创建⼀个30层的⽂件夹
import java. io. File ;
public class Demo2 {
public static void main ( String [ ] args) {
String [ ] names = { "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" } ;
StringBuilder path = new StringBuilder ( "C:\\Users\\22150\\Desktop" ) ;
for ( int i = 0 ; i < 30 ; i++ ) {
path. append ( names[ i% names. length] ) . append ( "\\" ) ;
}
boolean ret = new File ( path. toString ( ) ) . mkdirs ( ) ;
System . out. println ( ret) ;
}
}
【中】获取桌⾯上所有的⾮隐藏的⽂件(不要⽂件夹)
import java. io. File ;
public class Demo3 {
public static void main ( String [ ] args) {
File file = new File ( "C:\\Users\\22150\\Desktop" ) ;
File [ ] files = file. listFiles ( f-> ! f. isHidden ( ) && f. isFile ( ) ) ;
for ( File file1 : files) {
System . out. println ( file1) ;
}
}
}
【中】递归删除⼀个⾮空的⽂件夹
import java. io. File ;
public class Demo4 {
public static void main ( String [ ] args) {
File file = new File ( "Bigdataday20\\src\\file" ) ;
deleteDir ( file) ;
}
private static void deleteDir ( File file) {
File [ ] files = file. listFiles ( ) ;
for ( File f : files) {
if ( f. isFile ( ) ) {
f. delete ( ) ;
} else if ( f. isDirectory ( ) ) {
deleteDir ( f) ;
f. delete ( ) ;
}
}
file. delete ( ) ;
}
}
【难】列举⼀个⽂件夹中的所有的⽂件,及⼦⽂件夹中的⼦⽂件
格式如下:
| a
|----|b
|----|c
|----|----|a.txt
|----|----|b.txt
|----|d
|----|----|e.mp4
|----|----|f
|----|----|----|a.avi
import java. io. File ;
public class Demo5 {
public static void main ( String [ ] args) {
showDir ( "C:\\Users\\22150\\Desktop" ) ;
}
private static void showDir ( String file) {
showDir ( new File ( file) , 0 ) ;
}
private static void showDir ( File file, int level) {
StringBuilder builder = new StringBuilder ( "|" ) ;
for ( int i = 0 ; i < level; i++ ) {
builder. append ( "----|" ) ;
}
builder. append ( file. getName ( ) ) ;
System . out. println ( builder) ;
if ( file. isDirectory ( ) ) {
File [ ] files = file. listFiles ( ) ;
for ( File file1 : files) {
showDir ( file1, level + 1 ) ;
}
}
}
}
【难】设计⼀个⽅法,实现⼀个⽂件夹的拷⻉(包括⽂件夹中的⼦⽂件和⼦⽂件夹)
import java. io. * ;
public class Demo6 {
public static void main ( String [ ] args) {
copyDir ( new File ( "Bigdataday20\\src\\file" ) , new File ( "Bigdataday19\\src\\file" ) ) ;
}
private static void copyDir ( File src, File dst) {
dst. mkdirs ( ) ;
File [ ] files = src. listFiles ( ) ;
for ( File file : files) {
if ( file. isDirectory ( ) ) {
copyDir ( file, new File ( dst, file. getName ( ) ) ) ;
} else {
copyFile ( file, new File ( dst, file. getName ( ) ) ) ;
}
}
}
private static void copyFile ( File src, File dst) {
try ( BufferedInputStream bis = new BufferedInputStream ( new FileInputStream ( src) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( dst) ) ) {
byte [ ] array = new byte [ 1024 ] ;
int length = 0 ;
while ( ( length = bis. read ( ) ) != - 1 ) {
bos. write ( array, 0 , length) ;
bos. flush ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}