学习日志——2019/07/26

IO

  • File类
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;

public class ioDemo {
	public static void main(String[] args) throws IOException {
		File f=new File("d:\\b.txt");//相对路径
		File f1=new File("a.txt");//绝对路径
		System.out.println(f1.createNewFile());
//		
		File f2=new File("b");
		System.out.println(f2.mkdir());
		//创建指定文件夹,当文件夹所在目录不存在,则顺道一块创建
		File f3=new File("c\\d\\e");
		System.out.println(f3.mkdirs());
		
		//boolean delete() :当指定文件或文件夹存在时,删除指定文件或文件夹
		System.out.println(f3.delete());
		//获取文件信息
		String name=f1.getName();//获取文件名字
		long length=f1.length();//获取文件长度
		boolean hidden=f1.isHidden();//判断文件是否为隐藏文件
		System.out.println("文件名称:"+name);
		System.out.println("文件长度:"+length);
		System.out.println("该文件是隐藏文件?"+hidden);
		
	}
}

  • 使用FileOutPutStream类向文件word.txt写入信息,然后通过FileInputStream类将文件中的数据读取到控制台
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilePermission;

public class Filetest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("word.txt");
		try {
			FileOutputStream out =new FileOutputStream(file);
			byte buy[]="我有一只小毛驴,从来也不骑".getBytes();//创建byte型数组
			out.write(buy);			//将数组中的信息写入文件中
			out.close();			//将流关闭
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//创建FileInputStream类对象
		
		try {
			// 创建FileInputStream类对象
			FileInputStream in = new FileInputStream(file);
			byte byt[] = new byte[1024]; // 创建byte数组
			int len = in.read(byt); // 从文件中读取信息
			// 将文件中信息输出
			System.out.println("文件中的信息是:" + new String(byt, 0, len));
			in.close(); // 关闭流
		} catch (Exception e) {
			e.printStackTrace(); // 输出异常信息
		}
		
	}

}


在这里插入图片描述

  • FileReader和FileWriter类
    (由于汉字在文件中占两个字符,如果使用字节流读取不好可能会出现乱码现象,此时采用字符流Reader或Writer即可避免这种情况)
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class ftest extends JFrame { // 创建类,继承Jframe类
	private JScrollPane scrollPane;
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null; // 创建面板对象
	private JTextArea jTextArea = null; // 创建文本域对象
	private JPanel controlPanel = null; // 创建面板对象
	private JButton openButton = null; // 创建按钮对象
	private JButton closeButton = null; // 创建按钮对象
	
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}
	
	private JPanel getControlPanel() {
		if (controlPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(1);
			controlPanel = new JPanel();
			controlPanel.setLayout(flowLayout);
			controlPanel.add(getOpenButton(), null);
			controlPanel.add(getCloseButton(), null);
		}
		return controlPanel;
	}
	
	private JButton getOpenButton() {
		if (openButton == null) {
			openButton = new JButton();
			openButton.setText("写入文件"); // 修改按钮的提示信息
			openButton
					.addActionListener(new java.awt.event.ActionListener() {
						// 按钮的单击事件
						public void actionPerformed(ActionEvent e) {
							// 创建文件对象
							File file = new File("word.txt");
							try {
								// 创建FileWriter对象
								FileWriter out = new FileWriter(file);
								// 获取文本域中文本
								String s = jTextArea.getText();
								out.write(s); // 将信息写入磁盘文件
								out.close(); // 将流关闭
							} catch (Exception e1) {
								e1.printStackTrace();
							}
						}
					});
		}
		return openButton;
	}
	
	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("读取文件"); // 修改按钮的提示信息
			closeButton
					.addActionListener(new java.awt.event.ActionListener() {
						// 按钮的单击事件
						public void actionPerformed(ActionEvent e) {
							File file = new File("word.txt"); // 创建文件对象
							try {
								// 创建FileReader对象
								FileReader in = new FileReader(file);
								char byt[] = new char[1024]; // 创建char型数组
								int len = in.read(byt); // 将字节读入数组
								// 设置文本域的显示信息
								jTextArea.setText(new String(byt, 0, len));
								in.close(); // 关闭流
							} catch (Exception e1) {
								e1.printStackTrace();
							}
						}
					});
		}
		return closeButton;
	}
	
	public ftest() {
		super();
		initialize();
	}
	
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getControlPanel(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	
	public static void main(String[] args) { // 主方法
		ftest thisClass = new ftest(); // 创建本类对象
		thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thisClass.setVisible(true); // 设置该窗体为显示状态
	}
	/**
	 * @return
	 */
	protected JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getJTextArea());
		}
		return scrollPane;
	}
}

在这里插入图片描述

  • 带缓存的输入输出流
    • BufferedInputStream与BufferedOutputStream类
      • BufferedReader与BufferedWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class t1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String content[]= {"好久不见","最近好吗","常联系"};
		File file=new File("1.txt");
		try {
			FileWriter fw=new FileWriter(file);
			BufferedWriter bufw=new BufferedWriter(fw);
			for(int k=0;k<content.length;k++){
				bufw.write(content[k]);
				bufw.newLine();
			}
			bufw.close();
			fw.close();
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			FileReader fr=new FileReader(file);
			BufferedReader bufr=new BufferedReader(fr);
			String s=null;
			int i=0;
			//如果文件的文本行数不为null,则进入循环
			while((s=bufr.readLine())!=null) {
				i++;
				System.out.println("第"+i+"行"+s);
			}
			bufr.close();
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	

}

在这里插入图片描述

  • 数据输入/输出流
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class e1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			FileOutputStream fs=new FileOutputStream("work.txt");
			DataOutputStream ds=new DataOutputStream(fs);
			ds.writeUTF("使用writeUTF()方法写入数据:");	//写入磁盘文件数据
			ds.writeChars("使用writeChars写入数据:");
			ds.writeBytes("使用writeBytes写入数据:");
			ds.close();
			FileInputStream fis=new FileInputStream("work.txt");
			DataInputStream dis=new DataInputStream(fis);
			System.out.println(dis.readUTF());
			} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值