您的位置:首页 > 编程语言 > Java开发

java学习笔记(一)

2016-05-11 18:01 447 查看
输入输出流

File
使用路径字符串来创建File类实例,相关方法见API,文件路径可以是绝对路径,也可以是相对路径,但系统会根据用户的工作路径来解释相对路径,需要留意.

可以getName(),getAbsoluteFile(),getParent(),list()等等

IO流

分为输入流和输出流,其中输入流以InputStream和Reader作为基类,输出流以OutputStream和Writer作为基类,它们都属于抽象类,不可直接创建实例。

分为字节流和字符流,字节流操作单位是8位的字节,而字符流操作的单位是16位的字符。

分为节点流和处理流,其中节点流是直接访问数据源的底层操作,处理流是对底层的数据流进行封装,而且封装好的流非常好用。

流的概念详解

所有流都是从InputStream/Reader、OutputStream/Writer四个基类中派生,都提供方法记录指针移动

InputStream

int read() 读取单个字节,返回实际读取的字节,访问的字节数据直接转为int

int read(byte[] b) 最多读取b.length()个字节,存储在b中。

int read(byte[] b,int off,int len) 从输入流中最多读取len个字节,存储在b数组中,从off位置开始,返回实际读取的字符数量,这里是返回数量,不是read()放回读取的数据。

Writer

void write(int c) 将单个字符写入输出流

void write(byte[] buf) 将buf写入输出流

void write(byte[] buf,int off,int len) 将buf从off开始写入len个字符

其中字符流以字符作为操作单位,可以字符串来代替字符数组故Writer还包括

void write(String str)

void write(String str,int off,int len)

记得执行结束后显式关闭输出流close(),或者使用java7之后的try(){}cath{}语句自动关闭


Windows平台的换行符是\r\n,UNIX/Linux等平台为\n


常用的节点流

1. FileWriter/FileOutputStream,FileReader/FileInputStream

显然FileWriter是以字节方式,读取文本非常方便

import java.io.*;
public class FileWriterTest {
public static void main(String[] args)
{
try{
FileWriter fw = new FileWriter("poem.txt");
fw.write("小草\r\n");
fw.write("离离原上草\r\n");
fw.write("一岁一枯荣\r\n");
fw.write("野火烧不尽\r\n");
fw.write("春风吹又生\r\n");
}catch(IOException e)
{
e.printStackTrace();
}

}
}


2.ObjectOutputStream和ObjectInputStream

可以以二进制形式存储自定义的对象,注意该对象必须implements Serializable,读写时先用文件流,再用对象流进行处理

核心代码

//读入操作
FileInputStream fi = new FileInputStream(new File("对象.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
Student[] studentReader = (Student[])oi.readObject();
oi.close();


  

//写出操作
File file = new File("对象.txt");
FileOutputStream fo = new FileOutputStream(file);
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(student);
oo.close();


完整代码(使用windowsbuilder插件)

package Object;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.BevelBorder;

public class ObjectDemo {

private JFrame frame;
public Student[] student;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ObjectDemo window = new ObjectDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public ObjectDemo() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED, new Color(255, 0, 0), null, Color.RED, null));
scrollPane.setBounds(42, 10, 347, 152);
frame.getContentPane().add(scrollPane);

textArea = new JTextArea();
scrollPane.setViewportView(textArea);

JButton btnSave = new JButton("save");
btnSave.setBounds(42, 188, 93, 23);
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveActionPerformed();
}

});
frame.getContentPane().add(btnSave);

JButton btnRead = new JButton("read");
btnRead.setBounds(296, 188, 93, 23);
btnRead.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
readActionPerformed();
}
});
frame.getContentPane().add(btnRead);

student = new Student[]{new Student("小红","19"),new Student("小明","20")};
for(Student student1 : student)
{
this.textArea.append(student1.getName()+"\n");
this.textArea.append(student1.getAge()+"\n");
}
}
protected void saveActionPerformed()
{
try{
this.textArea.setText(null);
File file = new File("对象.txt");
FileOutputStream fo = new FileOutputStream(file);
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(student);
oo.close();
}catch(IOException ioe)
{
ioe.printStackTrace();
}
}
protected void readActionPerformed()
{
try{
FileInputStream fi = new FileInputStream(new File("对象.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
Student[] studentReader = (Student[])oi.readObject();
oi.close();
for(Student student : studentReader)
{
this.textArea.append(student.getName()+"\n");
this.textArea.append(student.getAge()+"\n");
}

}catch(Exception ioe)
{
ioe.printStackTrace();
}
}
}


View Code



好用的处理流

1.PrintStream

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class PrintStreamTest {
public static void main(String[] args)
{
try{
FileOutputStream fos = new FileOutputStream("PrintStreamTest.txt");
PrintStream ps = new PrintStream(fos);
ps.println("想写入什么就写入什么");
ps.println(new PrintStreamTest());
ps.close();

}catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}


非常强大的输出处理流,System.out就是PrintStream型,其print方法可以说是想写入什么就写入什么

而且只需关闭最上层的处理流,下层的会自动关闭。

2. 管道流

3.缓冲流

4.转换流

小结

计算机的文件通常分为文本文件和二进制文件,显然字节处理方式较强大,因为字节可以转为字符,但需要考虑用合适的方式来转为字符。比如Windows简体中文默认是GBK字符集,而linux下默认是UTF-8。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: