1.File类的基本用法:
/**
* 功能:File类的基本用法
*/
package com.test1;
import java.io.*;
public class Demo11_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
// File f=new File("d:\\aa.txt");
//
// //得到文件的路径
// System.out.println("文件路径:"+f.getAbsolutePath());
// //得到文件的大小
// System.out.println("文件大小:"+f.length());
// //System.out.println("可读",f.canRead()+f.);
// //创建文件或者创建文件夹,双斜杠表示转义
// File f1=new File("d:\\ff\\hsp.txt");
// if(!f1.exists())
// {
// //可以创建
// try {
// f1.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }else{
// System.out.println("有文件,不能创建");
// }
//
// File f2=new File("d:\\ff");
//
// if(f.isDirectory())
// {
// System.out.println("文件夹存在");
// }else{
// //可以创建
// f2.mkdir();
// }
//列出一个文件夹下面的所有文件
File f3=new File("d:/迅雷下载");
if(f3.isDirectory())
{
File lists[]=f3.listFiles();
for(int i=0;i<lists.length;i++)
{
System.out.println("文件名"+lists[i].getName());
}
}
}
}
2.FileInputStream类的使用:
/**
* 功能:演示FileInputStream 类的使用
*/
package com.test2;
import java.io.*;
public class Demo11_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//得到一个文件对象,f->d:\hsp.txt
File f=new File("d:\\hsp.txt");
FileInputStream fis=null;
//因为File没有读写的能力,所以需要的是个InputStream
try {
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte []bytes=new byte[1024];
int n=0;//得到实际读取到的字节数
//循环读取
while((n=fis.read(bytes))!=-1)//当文件读完后fis.read 返回-1
{
//把字节转成String
String s=new String (bytes,0,n);
System.out.println(s);
}
//fis.read(arg0)
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭文件流必须放这里,无论如何finally里面的语句
try {
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.FileOutputStream类的使用:
/**
* 功能:演示FileOutputStream
*/
package com.test3;
import java.io.*;
public class Demo11_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("d:\\ss.txt");
//字节输出流
FileOutputStream fos=null;
try{
fos=new FileOutputStream(f);
String s="苏苏,你不要这样!\r\n";
String s1="晴雪,我快抑制不住焚寂的煞气!";
// //定义一个字节数组
// byte []bytes=new byte[1024];
fos.write(s.getBytes());
fos.write(s1.getBytes());
}catch (Exception e){
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.图片文件的调用:
/**
* 图片拷贝
*/
package com.test4;
import java.io.*;
public class Demo12_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//思路,先把图片读入到内存-->写入到某个文件
//由于是二进制文件,因此只能用字节流完成
File f1=new File("c:\\a.jpg");
File f2=new File("d:\\aa.jpg");
//输入流
FileInputStream fis=null;
//输出流
FileOutputStream fos=null;
try {
fis=new FileInputStream(f1);
fos=new FileOutputStream(f2);
byte buf[]=new byte[512];
int n=0;//记录实际读取到的字节数
//循环读取
while((n=fis.read(buf))!=-1)
{
//输出到指定文件
fos.write(buf);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭打开的文件流
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.文件字符流(FileReader/FileWriter)
/**
* 演示文件字符流的案例
*/
package com.test5;
import java.io.*;
public class Demo12_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//文件取出字符流对象(输入流)
FileReader fr=null;
//写入到文件(输出流)
FileWriter fw=null;
try{
//创建fr对象
fr=new FileReader("d:\\hsp.txt");
fw=new FileWriter("g:\\hsp.txt");
int n=0;//记录实际读取的字符数
//读入到内存
//c[]相当于缓存
char c[]=new char[1024];
while((n=fr.read(c))!=-1)
{
// String s=new String(c,0,n);
// System.out.println(s);
fw.write(c,0,n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
fr.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
6.缓冲字符流(BufferedReader/BufferedWriter)
/**
* 演示缓冲字符流案例
*/
package com.test6;
import java.io.*;
public class Demo12_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=null;
BufferedWriter bw=null;
try{
//先创建一个FileReader对象,
FileReader fr=new FileReader("d:\\ss.txt");
br=new BufferedReader(fr);
//创建FileWriter 对象
FileWriter fw=new FileWriter("g:\\苏苏.txt");
bw=new BufferedWriter(fw);
//循环读取文件
String s="";
while((s=br.readLine())!=null)
{
//System.out.println(s);
//输出到磁盘
fw.write(s+"\r\n");
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try{
br.close();
bw.close();
}catch(Exception e){
e.printStackTrace();
}finally{};
}
}
}
7.综合实例(记事本开发)
/**
* 我的记事本(界面+功能)
*/
package com.test7;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NotePad extends JFrame implements ActionListener {
//定义所需要的组件
JTextArea jta=null;
//右侧滚动条
JScrollPane jsp=null;
//菜单条
JMenuBar jmb=null;
//第一个JMenu
JMenu jm1=null;
JMenu jm2=null;
//定义JMenuItem
JMenuItem jmi1=null;
JMenuItem jmi2=null;
JMenuItem jmi3=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
NotePad np=new NotePad();
}
//构造函数
public NotePad()
{
//创建jta
jta =new JTextArea();
jsp=new JScrollPane(jta);
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
//jm1=setMnemonic((int)'F');
jmi1=new JMenuItem("打开");
//注册监听
jmi1.addActionListener(this);
jmi1.setActionCommand("open");
//对保存菜单处理
jmi2=new JMenuItem("保存");
jmi2.addActionListener(this);
jmi2.setActionCommand("save");
//创建关闭菜单
jmi3=new JMenuItem("关闭");
jmi3.addActionListener(this);
jmi3.setActionCommand("close");
//把jm1加入到jmb
jmb.add(jm1);
//把item放入到Menu
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi3);
//放入到JFrame
this.add(jsp);
this.setJMenuBar(jmb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
this.setLocation(300,0);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//判断是哪个菜单被选中
if(arg0.getActionCommand().equals("open"))
{
//System.out.println("按下打开");
//隆重推荐JFileChooser
//文件选择组件
JFileChooser jfc1=new JFileChooser();
//设置名字
jfc1.setDialogTitle("请选择文件...");
//默认方式
jfc1.showOpenDialog(null);
//显示
jfc1.setVisible(true);
//得到用户选择文件的全路径
String filename=jfc1.getSelectedFile().getAbsolutePath();
//System.out.println(filename);
FileReader fr=null;
BufferedReader br=null;
BufferedWriter bw=null;
try{
fr=new FileReader(filename);
br=new BufferedReader(fr);
//从文件中读取信息并显示到jta
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
//放置加入jta
jta.setText(allCon);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else if(arg0.getActionCommand().equals("save"))
{
//出现保存对话框
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为...");
//按默认的方式显示
jfc.showSaveDialog(null);
jfc.setVisible(true);
//得到用户希望吧文件保存在何处,文件全路径
String file=jfc.getSelectedFile().getAbsolutePath();
//准备写入到指定文件
FileWriter fw=null;
BufferedWriter bw=null;
try{
fw=new FileWriter(file);
bw=new BufferedWriter(fw);
//自己优化
bw.write(this.jta.getText());
}catch(Exception e){
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else if(arg0.getActionCommand().equals("close"))
{
System.out.println("关闭窗口");
this.dispose();
}
}
}