例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. <php>Ajax基本格式

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  2. App上线流程全攻略(续)-iOS8之后的改动与所遇日常错误

    随着iOS8的公布,iTunes Connect的界面也是发生了非常大的改变,App 上传到 Store上面的步骤也是发生了些改变.以下继续用图说话: /*********************** ...

  3. [置顶] STM32移植contiki进阶之三(中):timer 中文版

    鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...

  4. angularjs基本执行流程

    近期温习了下angularjs执行流程,备记下.以便查看. 主要的执行流程例如以下: 1.用户请求应用起始页. 2.用户的浏览器向server发起一次HTTP连接,然后载入index.html页面,这 ...

  5. eclipse 集成maven插件

    本文转载自:http://www.blogjava.net/fancydeepin/archive/2012/07/13/eclipse_maven3_plugin.html 环境准备: eclips ...

  6. spark-shell启动集群

    使用spark-shell  启动spark集群时的流程简析: spark-shell->spark-submit->spark-class 在sprk-class中根据条件会从不同的入口 ...

  7. ios swift(1)冒泡排序

    //冒泡排序  稳定性最高  时间复杂度高 O(n(2)) ,交换次数太多, 一次交换等于三次赋值    最简单 var count = 0 func sortInts(inout data : [I ...

  8. ASP.NET MVC4 + Highcharts生成报表

    //后端 public ActionResult TighteningReport(BReportTighteningReportModel model, string rate, string we ...

  9. C++类中的静态成员变量与静态成员函数

    最近一直看c++相关的项目,但总是会被c++类中的静态成员变量与静态成员函数的理解感觉很是模糊,不明白为什么类中要是用静态成员变量.于是在网上搜集了一些资料,自己再稍微总结下. 静态成员的概念: 静态 ...

  10. Windows8.1使用博客客户端写博客

    1.首先去微软官网下载客户端(Windows live writer) http://windows.microsoft.com/zh-cn/windows-live/essentials 安装步骤 ...