java web io流,JavaIO流

IO流

文件--基本概念文件是数据源(保存数据的地方)的一种,比如大word文档、jpg文件、MP4文件...都是文件。文件最主要的作用就是保存数据,它既可以保存一张图片,也可以保存视频、声音...等

文件流--基本概念文件在程序中是以流的形式来操作的。

4b13aa5d204f3324994b8efa25bc21b2.png

流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径

如何判断是输入流、输出流?以内存为参照,如果数据流向内存流动,则是输入流;反之,则是输出流。

文件流--分类

07dac56176fbd67d1598b09876c1dced.png

java流分为两种流1、字节流:可以用于读写二进制文件及任何类型文件

2、字符流:可以用于读写文本文件,不能操作二进制文件

实例1.File类的使用/**

* File类的基本用法

*/

package com.io;

import java.io.*;

public class IO1 {

public static void main(String[] args) {

//创建一个文件对象

File f1 = new File("C:\\in.txt");

//得到文件路径

System.out.println("文件路径:" + f1.getAbsolutePath());

//得到文件的大小,字节数

System.out.println("文件大小:" + f1.length());

//是否可读

System.out.println("可读" + f1.canRead());

//创建文件和创建文件夹

File f2 = new File("C:\\in2.txt");

//判断文件是否存在

if(!f2.exists()){

//创建一个文件

try {

f2.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("文件创建成功");

} else {

System.out.println("文件已存在,无法创建");

}

File f3 = new File("C:\\file1");

//判断文件是否为文件夹

if(f3.isDirectory()){

System.out.println("文件夹已存在");

} else {

//创建文件夹

f3.mkdir();

System.out.println("文件夹已创建");

}

//列出一个文件夹下面的所有文件

File f4 = new File("C:\\");

if(f4.isDirectory()){

//获取文件数组

File[] lists = f4.listFiles();

for(int i = 0; i < lists.length; i++){

System.out.println("文件名:" + lists[i].getName());

}

}

}

}

实例2.文件字节流的使用/**

* FileInputStream类的使用

*/

package com.io;

import java.io.*;

public class IO2 {

public static void main(String[] args) {

//创建一个文件对象

File f = new File("C:\\in.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){

//把字节转换成String

String s = new String(bytes, 0, n);

System.out.println(s);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

//关闭文件流--必须放这里

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* FileOutputStream类的使用

*/

package com.io;

import java.io.*;

public class IO3 {

public static void main(String[] args) {

//创建文件对象

File f = new File("C:\\out2.txt");

//字节输出流

FileOutputStream fos = null;

try {

fos = new FileOutputStream(f);

String s = "hello,world\r\n";

String s2 = "hello,java\r\n";

//定义字节数组

//byte[] bytes = new byte[1024];

fos.write(s.getBytes());

fos.write(s2.getBytes());

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 图片拷贝

*/

package com.io;

import java.io.*;

public class IO4 {

public static void main(String[] args) {

//先把图片读入到内存 -> 写入到某个文件

//因为是二进制文件,因此只能用字节流完成

//输入流

FileInputStream fis = null;

//输出流

FileOutputStream fos = null;

try {

fis = new FileInputStream(new File("C:\\image01.jpg"));

fos = new FileOutputStream(new File("C:\\image01_copy.jpg"));

byte[] buf = new byte[1024];

//记录实际读取到的字节

int n = 0;

//循环读取

while((n = fis.read(buf)) != -1){

//输出到指定文件

fos.write(buf);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

//关闭打开的文件流

try {

fis.close();

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

实例3.文件字节流/**

* 字符流

*/

package com.io;

import java.io.*;

public class IO5 {

public static void main(String[] args) {

//文件读入字符流

FileReader fr = null;

//文件写出字符流

FileWriter fw = null;

try {

//创建文件读入字符流对象

fr = new FileReader(new File("C:\\test.txt"));

//创建文件写出字符流对象

fw = new FileWriter(new File("C:\\test_copy.txt"));

//读入到内存

//缓存char数组

char[] c = new char[1024];

//读入实际大小

int n = 0;

while((n = fr.read(c)) != -1){

fw.write(c, 0, n);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

//关闭文件流

try {

fr.close();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

实例4.缓存字节流/**

* 缓冲字符流

*

*/

package com.io;

import java.io.*;

public class IO6 {

public static void main(String[] args) {

//缓冲字符流定义

BufferedReader br = null;

BufferedWriter bw = null;

try {

//创建FileReader对象

FileReader fr = new FileReader(new File("C:\\test.txt"));

//创建FileWriter对象

FileWriter fw = new FileWriter(new File("C:\\test_copy2.txt"));

//创建缓冲字符流

br = new BufferedReader(fr);

bw = new BufferedWriter(fw);

//循环读文件

//临时字符串

String s = "";

while((s = br.readLine()) != null){

//输出到文件

bw.write(s + "\r\n");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

//关闭缓冲字符流

try {

br.close();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

实例5.记事本/**

* 记事本(界面+功能)

*/

package com.notepad;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class NotePad extends JFrame implements ActionListener{

//定义组件

//文本域

JTextArea jta = null;

//滚动条

JScrollPane jsp = null;

//菜单条

JMenuBar jmb =null;

//菜单栏目

JMenu jm = null;

//菜单项

JMenuItem jmi1 = null;

JMenuItem jmi2 = null;

//构造方法

public NotePad(){

//创建组件

jta = new JTextArea();

jsp = new JScrollPane(jta);

jmb = new JMenuBar();

jm = new JMenu("文件(F)");

jmi1 = new JMenuItem("打开(O)");

jmi2 = new JMenuItem("保存(S)");

//设置助记符

jm.setMnemonic('F');

jmi1.setMnemonic('O');

jmi2.setMnemonic('S');

//设置监听器

jmi1.addActionListener(this);

jmi2.addActionListener(this);

//设置动作监听器反应命令

jmi1.setActionCommand("open");

jmi2.setActionCommand("save");

//设置菜单条

setJMenuBar(jmb);

//把菜单栏目放入菜单条

jmb.add(jm);

//菜单项放入菜单栏

jm.add(jmi1);

jm.add(jmi2);

//加入到JFrame

add(jsp);

//设置窗体

setTitle("我的记事本");

setSize(400,300);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

NotePad np = new NotePad();

}

//动作监听器实现

@Override

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("open")){

//文件选择框

JFileChooser jfc = new JFileChooser();

jfc.setDialogTitle("打开文件");

jfc.showOpenDialog(null);

jfc.setVisible(true);

String file = jfc.getSelectedFile().getAbsolutePath();

//设置缓冲读入流

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(new File(file)));

//临时字符串

String s = "";

String all = "";

while((s = br.readLine()) != null){

//因为readLine方法会去掉回车换行

all += s + "\r\n";

}

jta.setText(all);

} catch (Exception e2) {

e2.printStackTrace();

} finally {

try {

br.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

} else if(e.getActionCommand().equals("save")){

//文件选择框

JFileChooser jfc = new JFileChooser();

jfc.setDialogTitle("保存文件");

jfc.showSaveDialog(null);

jfc.setVisible(true);

String file = jfc.getSelectedFile().getAbsolutePath();

//设置缓冲写出流

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter(new File(file)));

//临时存放JTextArea中的字符串

String s = jta.getText();

//将字符串按一行分割成字符串数组

String[] ss = s.split("\r\n");

//循环写入写出流

for(int i = 0; i < ss.length; i++){

bw.write(ss[i] + "\r\n");

}

} catch (Exception e2) {

e2.printStackTrace();

} finally {

try {

bw.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

} else {

System.out.println("无效动作");

}

}

}

----------参考《韩顺平.循序渐进学.java.从入门到精通》

----------参考《JDK_API_1_6_zh_CN》

57d0b113363e3c0149ba422cf420b450.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值