例9.1一个文件复制应用程序,将某个文件的内容全部复制到另一个文件。

import java.io.*;

public class Example9_1 {
public static void main(String arg[]) {
File inputFile = new File("file1.txt");
File outputFile = new File("file2.txt");
int ch;
try {
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
System.out.print("文件复制程序开始工作:");
while ((ch = in.read()) != -1) {
out.write(ch);
}
in.close();
out.close();
System.out.print("文件复制程序工作结束:");
} catch (FileNotFoundException e1) {
System.out.print("文件没有找到!" + e1);
} catch (IOException e2) {
System.out.print("File read Error:" + e2);
}
}
}

例9.2说明BufferedReader类的用法的应用程序。

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; class MyWindow extends JFrame implements ActionListener {
JTextArea text;
BufferedReader in;
JButton button;
FileReader file; MyWindow() {
super("缓冲式流的读取");
Container con = this.getContentPane();// 获得内容面板
con.setSize(100, 400);
con.setLayout(new BorderLayout());
button.addActionListener(this);
text = new JTextArea(20, 30);
text.setBackground(Color.cyan);
JScrollPane jsp = new JScrollPane(text);
con.add(jsp, BorderLayout.CENTER);
con.add(button, "South");
this.setVisible(true);
this.pack();
try {
File f = new File("Example9_3.java");// 指定文件
file = new FileReader(f);// 创建FileReader对象
in = new BufferedReader(file);// 接到BufferedReader类对象上
} catch (FileNotFoundException e1) {
text.setText("文件没有找到");
button.removeActionListener(this);
}
} public void actionPerformed(ActionEvent e) {
String s;
if (e.getSource() == button) {
try {
while ((s = in.readLine()) != null)// 按行输入
{
text.append(s + '\n');// 从文件读出的内容在文本区中输出
}
} catch (IOException exp) {
}
}
}
} public class Example9_2 {
public static void main(String args[]) {
MyWindow myWin = new MyWindow();
}
}

例9.3说明BufferedWriter类用法的应用程序。

import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; class MyWindow extends JFrame implements ActionListener {
JTextArea text;
JButton button;
FileWriter writefile;
BufferedWriter out; MyWindow() {
super("缓冲式样流的输出");
Container con = this.getContentPane();// 获得内容面板
text = new JTextArea(20, 30);
text.setBackground(Color.cyan);
button = new JButton("写文件");
button.addActionListener(this);
con.setLayout(new BorderLayout());
con.setSize(40, 40);
con.setVisible(true);
con.add(text, "Center");
con.add(button, "South");
try {
writefile = new FileWriter("line.txt");
out = new BufferedWriter(writefile);
} catch (IOException e) {
}
} public void actionPerformed(ActionEvent e) {
String s;
if (e.getSource() == button) {// 将文本区内容采用缓冲式输出到文件
try {
out.write(text.getText(), 0, (text.getText()).length());
out.flush();
text.setText(null);
System.exit(0);
} catch (IOException exp) {
text.setText("文件写出错!\n");
System.exit(-1);
}
}
}
} public class Example9_3 {
public static void main(String args[]) {
MyWindow myWin = new MyWindow();
myWin.pack();
}
}

例9.4一个文件随机读写的应用程序。

import java.io.*;

public class Example9_4 {
public static void main(String args[]) {
RandomAccessFile inOut = null;
long data[] = { 151, 278, 235, 206, -170, 32, 43, 21, 83, 47 };
try {
inOut = new RandomAccessFile("longData.dat", "rw");
for (int i = 0; i < data.length; i++) {
inOut.writeLong(data[i]);
}
for (int i = data.length - 1; i >= 0; i--) {
inOut.seek(i * 8);// long型数据占8个字节,第i个整数自i*8字节开始
System.out.print(" " + inOut.readLong() + ((i == 0) ? "\n" : ","));
}
inOut.close();
} catch (FileNotFoundException e1) {
System.out.println("文件找不到!" + e1);
} catch (IOException e2) {
System.out.println("文件读写错!" + e2);
}
}
}

例9.5应用程序逐行读取自己的源代码显示。

import java.io.*;

public class Example9_5 {
public static void main(String args[]) {
try {
RandomAccessFile file = new RandomAccessFile("Example9_5.java", "rw");
long fileCurPos = 0;
long fileLength = file.length();
while (fileCurPos < fileLength) {
String s = file.readLine();
System.out.println(s);
fileCurPos = file.getFilePointer();
}
file.close();
} catch (FileNotFoundException e1) {
System.out.println("文件找不到!" + e1);
} catch (IOException e2) {
System.out.println("文件读写错!" + e2);
}
}
}

例9.6应用程序利用文件对话框确定数据文件。

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter; public class Example9_6 {
public static void main(String args[]) {
FrameFileDialog f = new FrameFileDialog();
}
} class FrameFileDialog extends JFrame implements ActionListener {
JFileChooser filedialog = null;
JLabel label = new JLabel("", JLabel.CENTER);
JButton b1, b2;
JTextArea text; FrameFileDialog() {
super("带文件对话框的窗口");
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
con.setSize(40, 50);
JPanel p = new JPanel();
b1 = new JButton("打开文件");
b2 = new JButton("保存文件");
b1.addActionListener(this);
b2.addActionListener(this);
p.add(b1);
p.add(b2);
text = new JTextArea(20, 30);
JScrollPane jsp = new JScrollPane(text);
filedialog = new JFileChooser("D:\\workspace");
/* 建立一个JFileChooser对象,并指定目录为默认文件对话框路径 */
filedialog.setControlButtonsAreShown(true);// 显示打开和撤销按钮
filedialog.addChoosableFileFilter(new MyFileFilter("txt"));
filedialog.addChoosableFileFilter(new MyFileFilter("java"));
text.setBackground(Color.cyan);
con.add(jsp, BorderLayout.CENTER);
con.add(label, BorderLayout.NORTH);
con.add(p, BorderLayout.SOUTH);
this.setVisible(true);
this.pack();
} public void actionPerformed(ActionEvent e) {
File file = null;
int result;
if (e.getSource() == b1)// 打开文件
{
filedialog.setDialogTitle("打开文件");
result = filedialog.showOpenDialog(this);
text.setText("");
if (result == JFileChooser.APPROVE_OPTION) {
file = filedialog.getSelectedFile();
label.setText("你选择打开的文件名称是:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
label.setText("你没有选择任何文件");
}
FileInputStream fileStream = null;
if (file != null) {
try {
fileStream = new FileInputStream(file);
} catch (FileNotFoundException nfe) {
label.setText("文件没有找到");
return;
}
int readByte;
try {
while ((readByte = fileStream.read()) != -1) {
text.append(String.valueOf((char) readByte));
}
fileStream.close();
} catch (IOException ie) {
label.setText("读取文件出错");
}
}
} else if (e.getSource() == b2)// 保存文件
{
filedialog.setDialogTitle("保存文件");
result = filedialog.showSaveDialog(this);
file = null;
String fileName;
if (result == JFileChooser.APPROVE_OPTION) {
file = filedialog.getSelectedFile();
label.setText("你选择保存的文件名称是:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
label.setText("你没有选择任何文件");
}
FileOutputStream fileStream = null;
if (file != null) {
try {
fileStream = new FileOutputStream(file);
} catch (FileNotFoundException nfe) {
label.setText("文件没有找到");
return;
}
String content = text.getText();
try {
fileStream.write(content.getBytes());
fileStream.close();
} catch (IOException ie) {
label.setText("写文件出错");
}
}
}
}
} class MyFileFilter extends FileFilter {
String ext; MyFileFilter(String t) {
ext = t;
} public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String fn = file.getName();
int index = fn.lastIndexOf(',');
if (index > 0 && index < fn.length() - 1) {
String extension = fn.substring(index + 1).toLowerCase();
if (extension.equals(ext)) {
return true;
}
}
return false;
} public String getDescription() {
if (ext.equals("java")) {
return "JAVA Source File(*.java)";
}
if (ext.equals("txt")) {
return "TXT File(*.txt)";
}
return "";
}
}

04747_Java语言程序设计(一)_第9章_输入和输出流的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. 04747_Java语言程序设计(一)_第3章_面向对象编程基础

    链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...

  5. 04747_Java语言程序设计(一)_第1章_Java语言基础

    二进制0b开头 八进制0开头 十六进制0x开头 package com.jacky; public class Aserver { public static void main(String arg ...

  6. 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础

    例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...

  7. 04747_Java语言程序设计(一)_第8章_多线程

    例8.1应用程序用Thread子类实现多线程. import java.util.Date; public class Example8_1 { static Athread threadA; sta ...

  8. 04747_Java语言程序设计(一)_第7章_图形、图像与多媒体

    例7.1小应用程序用6种字型显示字符串,显示内容说明本身的字型. import java.applet.*; import java.awt.*; public class Example7_1 ex ...

  9. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...

随机推荐

  1. hdu 5424 Rikka with Graph II(dfs+哈密顿路径)

    Problem Description   As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...

  2. 【转】WCF、WebAPI、WCFREST、WebService之间的区别

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  3. SWTBOK測试实践系列(4) -- 软件測试技术的黑白之道

    白盒測试和黑盒測试往往是项目中最受争议的两种測试类型,每一个人偏爱各不同.现实生活中行业人员大多喜欢白盒測试而忽视黑盒測试,那么项目中又应该怎样平衡这两类測试呢?我们先来看两个案例. 案例一: 某移动 ...

  4. android避免service被杀

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建@Override    public int onStartComma ...

  5. Gson源码分析之Json结构抽象和注解使用

    github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...

  6. RMAN常用备份恢复命令汇总

    RMAN命令 1.独立命令  RMAN>shutdown immediate  RMAN>startup  RMAN>backup format 'd:\backup\%d_%s.b ...

  7. C# XML,XmlDocument简单操作实例

    private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml" ...

  8. 无法从带有索引像素格式的图像创建graphics对象(转)

    大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...

  9. Qt串口通信

    1. Qt串口通信类QSerialPort 在Qt5的的更新中,新增了串口通信的相关接口类QSerialPort,这使得在开发者在使用Qt进行UI开发时,可以更加简单有效地实现串口通信的相关功能. 开 ...

  10. 刷新 tableview

    UITableView对于iOS开发者来说一定不会陌生,很有可能你的APP很多界面都用到它.关于UITableView的文章,想必已经不计其数,没事可以多看看.特别是UITableView优化的文章, ...