Java输入输出处理技术1
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的更多相关文章
- Java输入输出处理技术2
7.从键盘输入 从键盘输入一行字符,并显示到屏幕上. package io; import java.io.*; public class ReadAndWrite { public static v ...
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
- Java 加解密技术系列文章
Java 加解密技术系列之 总结 Java 加解密技术系列之 DH Java 加解密技术系列之 RSA Java 加解密技术系列之 PBE Java 加解密技术系列之 AES Java 加解密技术系列 ...
- java的JSP技术
java的JSP技术 [toc] 1.JSP简介 Jsp技术是用来开发java web的页面显示的,所有MVC模型里面的视图层,所以视图层的开发 jsp不是编程语言,三个英文是java server ...
- java的poi技术写Excel的Sheet
在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...
- Java Web编程技术学习要点及方向
学习编程技术要点及方向亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理.要Jar, 不要War:以小为主,以简为宝,集堆而成.去繁取简 Spring Boot,明日之春(future of ...
- java的poi技术读,写Excel[2003-2007,2010]
在上一篇blog:java的poi技术读取Excel[2003-2007,2010] 中介绍了关于java中的poi技术读取excel的相关操作 读取excel和MySQL相关: java的poi技术 ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java编解码技术,netty nio
对于java提供的对象输入输出流ObjectInputStream与ObjectOutputStream,可以直接把java对象作为可存储 的字节数组写入文件,也可以传输到网络上去.对与java开放人 ...
随机推荐
- 【Java】 foreach对数组赋值问题
今天写代码时发现了如下问题: public class Test { public static void main(String[] args) { int[] arr= new int[5]; f ...
- eclipse中运行 main 方法报错,找不到类
eclipse (maven 项目)中运行 main 方法报错,找不到类 ** 发现:在 eclipse中的 "Marker" 控制面板中 ,发现问题所在 只要删除 maven 仓 ...
- 学会使用DNSPod,仅需三步
学会使用DNSPod,仅需三步 第一步:在DNSPod添加记录 1.访问 https://www.dnspod.cn网站,在DNSPod官网首页的右上角,有[注册],如下图所示,点击[注册]按钮 ...
- 深入理解ajax系列第八篇
前面的话 在以前,网站的用户与后端交互的主要方式是通过HTML表单的使用.表单的引入在1993年,由于其简单性和易用性,直到电子商务出现之前一直保持着重要位置.理解表单提交,对于更深入地理解ajax是 ...
- 机器学习之路: python 朴素贝叶斯分类器 MultinomialNB 预测新闻类别
使用python3 学习朴素贝叶斯分类api 设计到字符串提取特征向量 欢迎来到我的git下载源代码: https://github.com/linyi0604/MachineLearning fro ...
- ?P<username>\w+
- luoguP3480 [POI2009]KAM-Pebbles 阶梯Nim
将序列差分并翻转之后,变成了阶梯\(Nim\)的模板题 QAQ #include <cstdio> #include <cstring> #include <iostre ...
- [BZOJ4571][SCOI2016]美味(贪心+主席树)
经典问题,按位贪心,每次需要知道的是”在这一位之前的位都以确定的情况下,能否找到这一位是0/1的数”,这就是在询问[L,R]内某个值域区间是否有数,主席树即可. #include<cstdio& ...
- shellcode在栈溢出中的利用与优化
0x00 前言 在<Windows Shellcode学习笔记——shellcode的提取与测试>中介绍了如何对shellcode作初步优化,动态获取Windows API地址并调用,并通 ...
- play framework系列之打包war
概览 Play framwork 是我们一直在使用的框架,从刚开始的简单使用,乱起八糟的jar包引用,项目组成员之间的下载项目之后的引用问题等,遇到各种问题,我都一一解决,我将在这个系列中奉上解决方案 ...