1.保存用户输入到文件

从键盘读入一行字符,写到文件output.txt中去。

package io;
import java.io.*;
public class MyFileOutput { public static void main(String[] args) {
FileInputStream fin;
FileOutputStream fout;
int ch;
try {
fin=new FileInputStream(FileDescriptor.in);
fout=new FileOutputStream("output.txt");
System.out.println("请输入一行字符:");
while((ch=fin.read())!='\r')
fout.write(ch);
fin.close();
fout.close();
System.out.println("文件写入成功!");
} catch (FileNotFoundException e) {
System.out.println("不能创建文件!");
}catch(IOException e){
System.out.println("输出流有误!");
} } }

2.显示文件内容

显示文本文件的内容。

package io;
import java.io.*;
public class TypeFile { public static void main(String[] args) {
FileInputStream fin;
FileOutputStream fout;
int ch;
if(args.length<1){
System.out.println("请指定文件名!");
return;
}
try {
fin=new FileInputStream(args[0]);
fout=new FileOutputStream(FileDescriptor.out);
while((ch=fin.read())!=-1)
fout.write(ch);
fin.close();
fout.close();
} catch (FileNotFoundException e) {
System.out.println("文件没有找到!");
} catch (IOException e) {
System.out.println("输入流有误!");
} } }

3.文件的复制

文件复制程序。

package io;
import java.io.*;
public class CopyFile { public static void main(String[] args) {
FileInputStream fin;
FileOutputStream fout;
int ch;
if(args.length!=2){
System.out.println("参数格式不对,应该为:java CopyFile 原文件名 目标文件名");
return;
}
try {
fin=new FileInputStream(args[0]);
fout=new FileOutputStream(args[1]);
while((ch=fin.read())!=-1)
fout.write(ch);
fin.close();
fout.close();
System.out.println("文件复制成功!");
} catch (FileNotFoundException e) {
System.out.println("文件没有找到!");
} catch (IOException e) {
System.out.println("读写文件有误!");
} } }

4.顺序输入流

顺序输入流示例。

package io;
import java.io.*;
import java.util.*; public class FileList implements Enumeration{
String MyFileList[];
int current=0;
public FileList(String filelist[]){
MyFileList=filelist;
} public FileList(){
MyFileList=null;
} public boolean hasMoreElements() {
if(MyFileList==null)
return false;
if(current<MyFileList.length)
return true;
else
return false;
} public Object nextElement() {
FileInputStream in=null;
if(!hasMoreElements())
return null;
try {
in = new FileInputStream(MyFileList[current]);
current++;
} catch (FileNotFoundException e) {
System.out.println("Can't open file:"+MyFileList[current]);
}
return in;
} }
package io;
import java.io.*;
import java.util.*;
public class MySequenceIn { public static void main(String[] args) {
FileList myList = new FileList(args);
SequenceInputStream sin;
FileOutputStream fout;
int data;
try {
sin = new SequenceInputStream(myList);
fout = new FileOutputStream(FileDescriptor.out);
while((data=sin.read())!=-1)
fout.write(data);
sin.close();
fout.close();
} catch (FileNotFoundException e) {
System.out.println("文件无法打开");
} catch (IOException e) {
System.out.println("读写文件有误");
} } }

5.管道输入输出流

管道输入输出流示例。

package io;
import java.io.*;
//本线程类用于发送数据
public class ThreadOut extends Thread {
PipedInputStream pin;
PipedOutputStream pout;
byte data[]={1,2,3};
public ThreadOut(PipedInputStream in, PipedOutputStream out){
pin = in;
pout = out;
} public void run(){
try {
pout.write(data);
} catch (IOException e) {
// e.printStackTrace();
}
}
}
package io;
import java.io.*;
//本线程类用于接收数据
public class ThreadIn extends Thread {
PipedInputStream pin;
PipedOutputStream pout;
int data;
public ThreadIn(PipedInputStream in, PipedOutputStream out){
pin = in;
pout = out;
} public void run(){
try {
while((data=pin.read())!=-1)
System.out.println(data);
} catch (IOException e) {
// e.printStackTrace();
}
}
}
package io;
import java.io.*;
public class MyPipedIO { public static void main(String[] args) {
PipedInputStream mypin=null;
PipedOutputStream mypout=null;
try {
mypin = new PipedInputStream();
mypout = new PipedOutputStream();
mypin.connect(mypout);
ThreadOut tout = new ThreadOut(mypin, mypout);
ThreadIn tin = new ThreadIn(mypin, mypout);
tout.start();
tin.start();
} catch (IOException e) {
System.out.println("无法连接管道");
} } }

6.过滤输入输出流

数据输入输出流使用示例。

package io;
import java.io.*;
public class MyDataIO { public static void main(String[] args) {
DataOutputStream dout;
DataInputStream din;
try {
dout = new DataOutputStream(new FileOutputStream("testfile.dat"));
dout.writeInt(100);
dout.writeLong(123456789);
dout.writeDouble(1.23456);
dout.writeFloat(1.2f);
dout.writeBoolean(true); din = new DataInputStream(new FileInputStream("testfile.dat"));
System.out.println(din.readInt());
System.out.println(din.readLong());
System.out.println(din.readDouble());
System.out.println(din.readFloat());
System.out.println(din.readBoolean());
} catch (FileNotFoundException e) {
System.out.println("没有找到文件!");
} catch (IOException e) {
System.out.println("无法正常创建输入输出流数据!");
} } }

Java输入输出处理技术1的更多相关文章

  1. Java输入输出处理技术2

    7.从键盘输入 从键盘输入一行字符,并显示到屏幕上. package io; import java.io.*; public class ReadAndWrite { public static v ...

  2. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  3. Java 加解密技术系列文章

    Java 加解密技术系列之 总结 Java 加解密技术系列之 DH Java 加解密技术系列之 RSA Java 加解密技术系列之 PBE Java 加解密技术系列之 AES Java 加解密技术系列 ...

  4. java的JSP技术

    java的JSP技术 [toc] 1.JSP简介 Jsp技术是用来开发java web的页面显示的,所有MVC模型里面的视图层,所以视图层的开发 jsp不是编程语言,三个英文是java server ...

  5. java的poi技术写Excel的Sheet

    在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...

  6. Java Web编程技术学习要点及方向

    学习编程技术要点及方向亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理.要Jar, 不要War:以小为主,以简为宝,集堆而成.去繁取简 Spring Boot,明日之春(future of ...

  7. java的poi技术读,写Excel[2003-2007,2010]

    在上一篇blog:java的poi技术读取Excel[2003-2007,2010] 中介绍了关于java中的poi技术读取excel的相关操作 读取excel和MySQL相关: java的poi技术 ...

  8. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  9. java编解码技术,netty nio

    对于java提供的对象输入输出流ObjectInputStream与ObjectOutputStream,可以直接把java对象作为可存储 的字节数组写入文件,也可以传输到网络上去.对与java开放人 ...

随机推荐

  1. Python爬虫个人记录(二) 获取fishc 课件下载链接

    参考: Python爬虫个人记录(一)豆瓣250 (2017.9.6更新,通过cookie模拟登陆方法,已成功实现下载文件功能!!) 一.目的分析 获取http://bbs.fishc.com/for ...

  2. thinkphp调整框架核心目录think的位置

    thinkphp的核心目录即框架文件可以放在项目之外的目录,这点手册上有提到,放在项目之外的地方可以方便其他项目共用一个框架文件. 在入口文件的index.php中,在导入框架目录这一行,可以直接修改 ...

  3. STP协议树配置

    STP协议树作用 为了提高网络可靠性,交换网络中通常会使用冗余链路. 然而,冗余链路会给交换网络带来环路风险 并导致广播风暴以及MAC地址表不稳定等问题进而会影响到用户的通信质量. 生成树协议STP( ...

  4. python-arcade时钟

    最近开始学习arcade的图形库,感觉功能很丰富,尝试画了个时钟,显示如下: 贴上调整好的代码: import arcade import math,time SCREEN_WIDTH = 800 S ...

  5. 【基础知识】.Net基础加强 第四天

    一. 显示实现接口 1. 显示实现接口的目的:为了解决法方法重名的问题. 2. 显示实现接口必须是私有的,不能用public 3. (复习)类中成员不写访问修饰符默认是private:类如果不写访问修 ...

  6. [leetcode sort]56. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. python脚本 pyqt 打包成windows可执行exe文件 pyinstaller

    今天学习pyqt,做了一些好玩的东西. 好奇之中想试试python脚本编译成可执行文件,一顿查询之后成功了! 我的环境是: windows10 64bit    python3.5    pyqt5 ...

  8. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  9. SDWebImage支持WebP格式图片

    SDWebImage本身就已经支持了webp格式的图片 1.下载libwebp https://github.com/webmproject/libwebp 然后你需要先安装好有homebrew或者m ...

  10. Notepad++源代码阅读——窗口元素组织与布局

    1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...