java IO
递归 利用递归解决阶乘问题 尽量不要使用
public class Recursion {
public static void main(String
[] args
) {
Recursion recursion
= new Recursion();
System
.out
.println(recursion
.recursion(2));
}
public int recursion(int num
){
if (num
== 1){
return 1;
}else {
return num
*recursion(num
-1);
}
}
}
IO流
FileInputStream FileOutputStream 字节传输byte 文件流
public class DemoFile {
public static void main(String
[] args
) {
try {
FileInputStream inputStream
= new FileInputStream(
new File("F:\\iso","CentOS-6.10-x86_64-bin-DVD1.iso"));
FileOutputStream outputStream
= new FileOutputStream(
new File("D:\\IdeaProjects\\bilibiliSpring","CentOS.iso"));
byte[] bytes
= new byte[1024*1024];
int len
;
long startime
= System
.currentTimeMillis();
while ((len
= inputStream
.read(bytes
)) != -1){
System
.out
.println(len
);
outputStream
.write(bytes
,0,len
);
}
long stoptime
= System
.currentTimeMillis();
System
.out
.println(stoptime
- startime
);
inputStream
.close();
outputStream
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
BufferedInputStream BufferedOutputStream 缓冲流提升读写速度
public class DemoBuffer {
public static void main(String
[] args
) {
try {
InputStream fileInputStream
= new FileInputStream(
new File("F:\\iso\\CentOS-6.10-x86_64-bin-DVD1.iso")
);
OutputStream fileOutputStream
= new FileOutputStream(
new File("D:\\IdeaProjects\\bilibiliSpring\\CentOS.iso")
);
BufferedInputStream bis
= new BufferedInputStream(fileInputStream
);
BufferedOutputStream bos
= new BufferedOutputStream(fileOutputStream
);
byte[] bytes
= new byte[1024*1024*32];
int len
;
long startime
= System
.currentTimeMillis();
while ((len
= bis
.read(bytes
)) != -1){
System
.out
.println(len
);
bos
.write(bytes
,0,len
);
}
long stoptime
= System
.currentTimeMillis();
System
.out
.println("耗时——>" + (stoptime
-startime
) + "ms");
bos
.close();
bis
.close();
fileInputStream
.close();
fileOutputStream
.close();
} catch (FileNotFoundException e
) {
e
.printStackTrace();
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
InputStreamReader OutputStreamWriter 字符流 对字符进行操作 txt .java
public class DemoInputStreamReader {
public static void main(String
[] args
) throws IOException
{
FileInputStream fileInputStream
= new FileInputStream("烂七八糟记事本.txt");
FileOutputStream fileOutputStream
= new FileOutputStream("copy.txt");
InputStreamReader isr
= new InputStreamReader(fileInputStream
,"UTF-8");
OutputStreamWriter osw
= new OutputStreamWriter(fileOutputStream
,"UTF-8");
char[] buffer
= new char[1024];
int len
;
while ((len
= isr
.read(buffer
)) != -1){
osw
.write(buffer
);
}
osw
.close();
isr
.close();
}
}