JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流
/**
* File类:文件的创建、删除、重命名、得到路径、创建时间等,是唯一与文件本身有关的操作类
*/
public class Main { public static void main(String[] args) {
//File.separator 表示分隔符
File f1 = new File("c:" + File.separator + "fuck" + File.separator + "javaTest1.txt");
String s1 = File.pathSeparator; //路径分隔符
System.out.println(File.separator + " " + s1); boolean b1 = f1.exists(); //文件是否存在
System.out.println(b1);
if (!b1) {
try {
boolean bt1 = f1.createNewFile(); //创建文件
System.out.println(bt1);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(f1.delete()); //删除文件 //System.out.println(""); 快捷键:打sout,然后中按Tab键
System.out.println(f1.getParent()); //得到文件的上一级路径 System.out.println(f1.isDirectory()); //判断是否是目录 File f2 = new File("c:" + File.separator + "fuck" + File.separator);
String[] fname = f2.list(); //列出文件夹中的所有文件名
for(String i:fname) System.out.println(i); File[] files = f2.listFiles(); //列出文件中的所有文件,以file数组返回
for(File i:files) System.out.println(i.getName()+" "+i.length()); File f3 = new File("c:\\fuck\\JavaTest1");
System.out.println(f3.mkdir()); //创建文件夹
f3.delete(); System.out.println(f3.renameTo(new File("c:\\fuck\\JavaTest2"))); //重命名
}
}
/**
* 在某个目录中找到某个扩展名的所有文件
*/
public class Main {
private static int num = 0;
public static void findFile(File f,String extName){
if(f==null)
return;
else{
if(f.isDirectory()){
File[] fs = f.listFiles();
if(fs!=null){
for(File i:fs)
findFile(i,extName);
}
}else{
String path = f.getPath().toLowerCase();
if(path.endsWith(extName)){
System.out.println(f.getPath());
++num;
}
}
}
}
public static void main(String[] args) {
File f = new File("c:\\fuck\\");
String extName = ".cpp";
findFile(f,extName);
System.out.println(num);
}
}
/**
* IO流:输入输出流
* 流:一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
* 流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
* 根据处理数据类型的不同分为:1.字符流 2.字节流
* 根据数据流向不同分为:1.输入流(程序从外部读取) 2.输出流(程序将数据写到外部)
*
* OutputStream类:接受输出字节并将这些字节发送到某个接收器
*/
public class Main { public static void main(String[] args) {
//write1();
write2();
System.out.println("finished.");
} /**
* 字节输出流方式一:每次输出一个字节
*/
public static void write1(){
OutputStream out = null;
try {
out = new FileOutputStream("c:\\fuck\\javaTest1.txt");
String info = "helloIO";
byte[] bs = info.getBytes();
for(int i=0;i<bs.length;++i)
out.write(bs[i]); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} /**
* 字节输出流方式二:每次输出指定大小字节
*/
public static void write2(){
OutputStream out = null;
try {
out = new FileOutputStream("c:\\fuck\\javaTest1.txt",true); //参数true表示追加输出
String info = "hello fish7";
byte[] bs = info.getBytes();
//out.write(bs);
out.write(bs,0,5); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public class Main { public static void main(String[] args) {
//read1();
// read2();
read3();
System.out.println("finished.");
} /**
* 字节输入流的读取方式一:每次读取一个字节
*/
public static void read1(){
InputStream in = null;
try {
in = new FileInputStream("c:\\fuck\\javaTest1.txt");
int bs = -1; //定义一个字节,-1表示没有数据
while((bs = in.read())!=-1){
System.out.print((char)bs);
}
System.out.println("");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} /**
* 字节输入流的读取方式二:一次性读取所有字节(适合不太大的文件)
*/
public static void read2(){
InputStream in = null;
try {
File f = new File("c:\\fuck\\javaTest1.txt");
in = new FileInputStream(f);
byte[] bs = new byte[(int)f.length()]; //根据文件大小构造字节数组
int len = in.read(bs);
System.out.println(new String(bs));
System.out.println("len = "+len); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} /**
* 字节输入流的读取方式三:每次读取指定大小的字节(折中的方法)
*/
public static void read3(){
InputStream in = null;
try {
File f = new File("c:\\fuck\\javaTest1.txt");
in = new FileInputStream(f);
byte[] bs = new byte[5];
int len = -1; //每次读取的实际长度
StringBuilder sb = new StringBuilder();
while((len=in.read(bs))!=-1){
sb.append(new String(bs,0,len));
}
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
/**
* 字符流:底层也是用字节流实现的
*/
public class Main { public static void main(String[] args) {
writer1();
System.out.println("finished.");
} /**
* 字符输出流方式一:以字符数组方式输出
*/
public static void writer1(){
Writer out = null;
try {
File f = new File("c:\\fuck\\javaTest1.txt");
out = new FileWriter(f,true); //true表示追加方式
String info = "good good study, day day up.";
//out.write(info.toCharArray());
out.write(info); } catch (IOException ex) {
ex.printStackTrace();
} finally{
try{
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
/**
* 字符流:底层也是用字节流实现的
*/
public class Main { public static void main(String[] args) {
//byteReader(); //输出一堆乱码
reader1();
System.out.println("finished.");
} /**
* 字符输入流方式一:使用指定大小的字符数组输入
*/
public static void reader1(){
File f = new File("c:\\fuck\\javaTest1.txt");
try {
Reader in = new FileReader(f);
char[] cs = new char[10];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len=in.read(cs))!=-1){
sb.append(new String(cs,0,len));
}
in.close();
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} /**
* 使用字节流读取文本文件
*/
public static void byteReader(){
File f = new File("c:\\fuck\\javaTest1.txt");
try {
InputStream in = new FileInputStream(f);
byte[] bs = new byte[10];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len=in.read(bs))!=-1){
sb.append(new String(bs,0,len));
}
in.close();
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 字符流:底层也是用字节流实现的
* :根据指定的编码,将1个或多个字节转化为java里的unicode字符,然后进行操作
* 字符操作一般用Writer,Reader等,字节操作一般用InputStream,OutputStream以及各种包装类,比如:
* BufferedInputStream和BufferedOutputStream等
* 总结:如果你确认你要处理的流是可打印的字符,那么使用字符流会简单些,如果不确认,那么用字节流总是不会错的。
*/
public class Main { public static void main(String[] args) {
//byteReader(); //输出一堆乱码
reader1();
System.out.println("finished.");
} /**
* 字符输入流方式一:使用指定大小的字符数组输入
*/
public static void reader1(){
File f = new File("c:\\fuck\\javaTest1.txt");
try {
Reader in = new FileReader(f);
char[] cs = new char[10];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len=in.read(cs))!=-1){
sb.append(new String(cs,0,len));
}
in.close();
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} /**
* 使用字节流读取文本文件
*/
public static void byteReader(){
File f = new File("c:\\fuck\\javaTest1.txt");
try {
InputStream in = new FileInputStream(f);
byte[] bs = new byte[10];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len=in.read(bs))!=-1){
sb.append(new String(bs,0,len));
}
in.close();
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 指定一个盘符下的文件,把该文件复制到指定的目录下。
*/
public class Main { public static void main(String[] args) {
copyFile("c:\\fuck\\javaTest1.txt","c:\\fuck\\javaTest2.txt");
copyFile("c:\\fuck\\ning.bmp","c:\\fuck\\ning2.txt"); //把.txt改成.bmp就可以看了
System.out.println("finished.");
} public static void copyFile(String src,String des){
File f1 = new File(src);
File f2 = new File(des);
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f1);
out = new FileOutputStream(f2);
byte[] bs = new byte[105];
int len = -1;
while((len=in.read(bs))!=-1){
out.write(bs,0,len);
} } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
in.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} }
}
/**
* 转换流:将一个字节流转换为字符流,也可以将一个字符流转换为字节流
* OutputStreamWriter:可以将输出的字符流转换为字节流的输出形式,可使用指定的charset将要写入流
* 中的字符编码成字节。
* InputStreamReader:将输入的字节流转换为字符流输入形式,使用指定的charset读取字节并将其解码为
* 字符。
*/
public class Main { public static void main(String[] args) {
//writer();
reader();
System.out.println("finished.");
} public static void writer(){ //把字符流转成字节流
try {
OutputStream out = new FileOutputStream("c:\\fuck\\javaTest1.txt");
String info = "你好吗abc.";
Writer w1 = new OutputStreamWriter(out); //通过字节输出流构造一个字符输出流
w1.write(info); w1.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} public static void reader(){ //把字节流转成字符流
try {
InputStream in = new FileInputStream("c:\\fuck\\javaTest1.txt");
Reader r1 = new InputStreamReader(in);
char[] cs = new char[105];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len=r1.read(cs))!=-1){
sb.append(new String(cs,0,len));
}
r1.close();
in.close();
System.out.println(sb); } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流的更多相关文章
- Java基础知识强化之IO流笔记09:File类功能
详见如下: Android(java)学习笔记87:File类使用
- java之io之file类的常用操作
java io 中,file类是必须掌握的.它的常用api用法见实例. package com.westward.io; import java.io.File; import java.io.IOE ...
- Java IO体系之File类浅析
Java IO体系之File类浅析 一.File类介绍 位于java.io下的Java File类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等.File对 ...
- Android(java)学习笔记87:File类使用
package cn.itcast_01; import java.io.File; /* * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式. * 而Java就提供了一个类File供我们使用 ...
- java学习笔记IO之File类
File类总结 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times } p.p2 { margin: 0.0px 0.0px 0.0p ...
- Java学习笔记36(File类)
File类可以对操作系统中的文件进行操作: File类的静态成员变量: package demo; import java.io.File; public class FileDemo { publi ...
- java学习笔记27(File类)
File类: 定义:文件和目录径的抽象表示形式, Java中将路径或者文件封装成File对象 1.File类的静态成员变量 package com.zs.Demo2; import java.io.F ...
- Android(java)学习笔记26:File类的使用
1. File类的使用 package cn.itcast_01; import java.io.File; /* * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式. * 而Java就提供 ...
- Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
第一讲 File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...
随机推荐
- PHP中PDO关闭连接的问题
在之前我们手写 mysql 的连接操作时,一般都会使用 mysql_close() 来进行关闭数据库连接的操作.不过在现代化的开发中,一般使用框架都会让我们忽视了底层的这些封装,而且大部分框架都已经默 ...
- Java基础系列(3)- HelloWorld详解
HelloWorld 1.新建一个java文件 文件后缀名为.java Hello.java [注意点]系统可能没有显示文件后缀名,我们需要手动打开 2.编写代码 public class Hello ...
- phpstoem破解
https://blog.csdn.net/voke_/article/details/78794567
- django使用restframework序列化查询集合(querryset)
第一: pip install djangorestframework 第二: 在setting.py文件中的app添加名为: 'rest_framework', 第三:再项目的APP下面新建名为(可 ...
- P3426-[POI2005]SZA-Template【KMP】
正题 题目链接:https://www.luogu.com.cn/problem/P3426 题目大意 给出一个长度为\(n\)的字符串\(s\),求一个长度最小的字符串\(t\)使得\(s\)所有\ ...
- 四、mybatis动态sql
# 前言前面mysql都是通过静态sql进行查询的,但是如果业务复杂的时候,我们会遇到引号问题,或者多一个空格,这就使得sql代码编写错误了,所以为了解决这个问题,我们有了动态sql. Mybatis ...
- node.js及npm安装&配置
之前我们在文言文安装教程里写过node.js及npm的安装,这里我们详细写一下. 下载node.js node.js下载分为两种,官网nodejs.org,和国内官网nodejs.cn,国内的童鞋建议 ...
- 3-等待线程终止的join方法
等待线程终止的join方法 在项目实践中经常会遇到一个场景,就是需要等待某几件事完成之后才能继续往下执行,比如线程加载资源等等. package com.heiye.learn1; public cl ...
- MySQL MVCC原理深入探索
一.MVCC的由来 二.MVCC的实际应用 RR级别场景 RC级别场景 三.MVCC的实现 3.1 多版本的数据从哪里来--Undo Log 3.1.1 插入操作对应的undo log 3.1.2 删 ...
- 反调试——7——CRC检测
反调试--7--CRC检测 CRC32: CRC的全称是循环冗余校验,作用是为了检测数据的完整性. CRC32的检测原理: 程序被编译后,代码段是固定的,因为已经被写死了. 我们在调试程序的时候,打断 ...