目录
- File类
- 常用方法
- 代码示例
- 流
- 分类
- 字节流输入流
- 字节流输出流
- 字节流复制粘贴效果
- 字符流输入流
- 字符流输出流
- Buff版输入输出流
- 二进制流
- 序列化和反序列化
File类
File file = new File( String pathname );
常用方法
代码示例
public static void main(String[] args) {
//1.创建文件对象
//File studentFile = new File("files");
File studentFile = new File("files/studentInfo.txt");
System.out.println("文件是否存在?" + studentFile.exists());
System.out.println("是否是一个文件?" + studentFile.isFile());
System.out.println("是否是一个目录?" + studentFile.isDirectory());
String path = studentFile.getPath();
System.out.println("相对路径是:" + path);
String absolutePath = studentFile.getAbsolutePath();
System.out.println("绝对路径是:" + absolutePath);
System.out.println("文件的名称是:" + studentFile.getName());
System.out.println("删除文件:" + studentFile.delete());
System.out.println("删除之后该文件是否存在?" + studentFile.exists());
try {
System.out.println("重新创建studentInfo.txt文件:" + studentFile.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
File kd50File = new File("files/kd50.txt");
System.out.println("kd50.txt的文件大小是:" + kd50File.length());
File newFile = new File("files/kgc/kd50/wang");
if(!newFile.exists()){
//newFile.mkdir();//创建文件夹的
newFile.mkdirs();//级联创建文件夹
}
System.out.println(newFile.exists());
System.out.println("----------------------------------");
/*File directory = new File("files");
String[] files = directory.list();
for (String s : files) {
System.out.println(s);
}*/
System.out.println("----------------------------------");
/*File directory = new File("files");
File[] files = directory.listFiles();
for (File file : files) {
System.out.println(file.getName() + " -------- " + file.isFile());
}*/
System.out.println("----------------------------------");
/*File directory = new File("files");
File[] files = directory.listFiles(File::isFile);
for (File file : files) {
System.out.println(file.getName() + " -------- " + file.isFile());
}*/
System.out.println("----------------------------------");
File directory = new File("files");
File[] files = directory.listFiles(File::isDirectory);
for (File file : files) {
System.out.println(file.getName() + " -------- " + file.isDirectory());
}
}
流
通过流来读写文件:流是一组有序的数据序列,以先进先出方式发送信息的通道
分类
当然除了这些之外还有二进制流、对象流…
字节流输入流
public class MyFileInput {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\KD50\\project\\API\\files\\stuedntInfo.txt");
//读取内容
int read;
while ((read = fis.read()) != -1) {
System.out.print((char) read);
}
System.out.println();
System.out.println("文件读取完毕");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节流输出流
public class MyFileOutput {
public static void main(String[] args) {
String words = "HelloWorld!!!";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("files/code.txt");
byte[] bytes = words.getBytes();
fos.write(bytes,0,bytes.length);
fos.flush();
System.out.println("内容输出完毕!!!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节流复制粘贴效果
public class CopyFile {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("files/code.txt");
fos = new FileOutputStream("files/copy.txt",true);
int read ;
while ((read = fis.read())!=-1){
fos.write(read);
}
fos.flush();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字符流输入流
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("files/studentInfo.txt");
int read;
while ((read=fr.read())!=-1){
System.out.print((char)read);
}
System.out.println("读取完毕");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流输出流
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("files/kgc/info.txt");
String words = "Hello World!!!\n" +
"大吉大利,今晚吃鸡!!!";
fw.write(words);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fw!=null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Buff版输入输出流
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader("files/studentInfo.txt");
br = new BufferedReader(fr);
String line;
while ((line = br.readLine())!=null){
System.out.println(line);
}
System.out.println("内容读取完毕!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
fr = new FileReader("files/studentInfo.txt");
br = new BufferedReader(fr);
fw = new FileWriter("files/kgc/studentInfo.txt");
bw = new BufferedWriter(fw);
String line;
while ((line=br.readLine())!=null){
bw.write(line + "\n");
}
System.out.println("复制完毕");
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二进制流
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
List<String> typeList = new ArrayList<>();
Collections.addAll(typeList,".jpg",".png");
File file = new File("files");
File[] files = file.listFiles(File::isFile);
try {
for (File f : files) {
if(typeList.contains(f.getName().substring(f.getName().lastIndexOf(".")))){
fis = new FileInputStream(file.getPath()+File.separator+f.getName());
dis = new DataInputStream(fis);
fos = new FileOutputStream(file.getPath()+File.separator+"images"+File.separator+f.getName());
dos = new DataOutputStream(fos);
int read;
while ((read=dis.read())!=-1){
dos.write(read);
}
dos.flush();
}
}
System.out.println("文件全部复制完毕");
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
序列化和反序列化
- 注册:先读取文件中的对象(反序列化),如果为空就创建一个新的Map,不为空就将读取的流转为Map对象,每当用户注册后,将信息写到Map里,然后再把Map序列化到文件中,以实现用户注册后信息能够持久化起来。
- 登录时从Map中进行校验
public class Menu {
Scanner sc = new Scanner(in);
static Map<String,User> userMap;
static{
InputStream is = null;
ObjectInputStream ois = null;
try {
is = new FileInputStream("files/user/userMap.txt");
ois = new ObjectInputStream(is);
userMap = (Map<String,User>)ois.readObject();
}catch (FileNotFoundException e){
userMap = new HashMap<>();
//e.printStackTrace();
}catch (IOException e){
userMap = new HashMap<>();
//e.printStackTrace();
} catch (ClassNotFoundException e) {
userMap = new HashMap<>();
//e.printStackTrace();
} finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void register(){
System.out.print("请输入用户名:");
String userCode = sc.next();
System.out.print("请输入密码:");
String userPassword = sc.next();
User user = new User(userCode,userPassword);
userMap.put(userCode,user);
//将注册的信息序列化
oosObject();
}
public User login(){
System.out.println("***************用户登录***************");
System.out.print("请输入用户名:");
String userCode = sc.next();
System.out.print("请输入密码:");
String userPassword = sc.next();
if(!userMap.containsKey(userCode) || !userPassword.equals(userMap.get(userCode).getUserPassword())){
return null;
}
return userMap.get(userCode);
}
private void oosObject(){
//将注册的信息保存到外部文件中
ObjectOutputStream oos = null;
OutputStream os = null;
try {
os = new FileOutputStream("files/user/userMap.txt");
oos = new ObjectOutputStream(os);
oos.writeObject(userMap);
oos.flush();
}catch (IOException e){
e.printStackTrace();
}finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
用户实体类
public class User implements Serializable {
private String userCode;
private String userPassword;
public User(String userCode, String userPassword) {
this.userCode = userCode;
this.userPassword = userPassword;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"userCode='" + userCode + '\'' +
", userPassword='" + userPassword + '\'' +
'}';
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
测试
public static void main(String[] args) {
Menu menu = new Menu();
//menu.register();
User user = menu.login();
if(user!=null){
System.out.println("登录成功,登录信息为:" + user);
}else {
System.out.println("登录失败!");
}
}