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

Java输入输出处理技术2

2015-01-19 16:38 87 查看
7.从键盘输入

从键盘输入一行字符,并显示到屏幕上。

package io;
import java.io.*;
public class ReadAndWrite {

public static void main(String[] args) {
InputStreamReader isr=new InputStreamReader(System.in);
OutputStreamWriter osr=new OutputStreamWriter(System.out);
int ch;
try {
System.out.print("请输入一行字符:");
while((ch=isr.read())!='\r')
osr.write(ch);
isr.close();
osr.close();
} catch (IOException e) {
System.out.println("输入流有误!");
}

}

}


8.文件复制

实现文件的复制。

package io;
import java.io.*;
public class ReadAndWriteFile {

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

}

}


9.控制台输入类Scanner

Scanner使用示例

package io;
import java.util.*;
public class useScanner {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name=in.nextLine();
System.out.println("请输入你的年龄:");
int age=in.nextInt();
System.out.println("请输入你的身高(单位:米):");
double height=in.nextDouble();
System.out.println("姓名:"+name+"年龄:"+age+"身高:"+height);
}

}


10.File类读取文件列表

列出目录下的所有文件和子目录。

package io;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class dir {

public static void main(String[] args) {
File fdir; //声明File对象
File[] AllFile; //声明一个File数组对象
String name; //定义一个String类型的name变量,存储文件名信息
String dirFlag; //标记是否为文件
long size; //存储文件长度信息
Date date; //存储文件属性中时间信息
if(args.length<1)
fdir = new File("."); //用户没有指定目录,则默认为当前目录
else
fdir = new File(args[0]);
AllFile=fdir.listFiles(); //获得文件和目录列表
//输出目录下所有的文件和目录
for(int i=0;i<AllFile.length;i++){
name = AllFile[i].getName(); //获得文件名
if(AllFile[i].isFile()){  //判断是否为文件
dirFlag="                ";
size=AllFile[i].length();
}else{
dirFlag="        <dir>        ";
size = 0;
}
date = new Date(AllFile[i].lastModified()); //获取文件最后一次修改时间
//对输出的文件格式进行格式化处理,输出24小时格式的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
//按照格式输出文件时间
System.out.print(sdf.format(date));
//输出目录标志
System.out.print(dirFlag);
if(size>0)
System.out.printf("%10d",size);
else
System.out.print("          ");
//输出文件名并换行
System.out.println(" "+name);
}
System.out.println("共有"+AllFile.length+"个文件和目录");
}

}


11.RandomAccessFile类进行文件加密

文件加密/解密示例。

package io;
import java.io.*;
public class encrypt {
private File file; //存储文件对象信息
byte[] buf;  //缓冲区,存储文件中的所有数据
RandomAccessFile fp;
//用参数filename指定的文件构造一个filed对象存储
//同时为缓冲区buf分配与文件长度相等的存储空间
public encrypt(String filename){
file=new File(filename);
buf=new byte[(int)file.length()];
}

public encrypt(File destfile){
file = destfile;
buf = new byte[(int)file.length()];
}

//按照读写的方式打开文件
public void openFile()throws FileNotFoundException{
fp=new RandomAccessFile(file,"rw");
}

//关闭文件
public void closeFile()throws IOException{
fp.close();
}

//对文件进行加密/解密
public void coding()throws IOException{
//将文件内容读到缓冲区中
fp.read(buf);
//将缓冲区中的内容取反
for(int i=0;i<buf.length;i++)
buf[i]=(byte)(~buf[i]);
//将文件指针定位到文件头部
fp.seek(0);
//将缓冲区中的内容写入到文件中
fp.write(buf);
}

public static void main(String[] args) {
encrypt oa;
if(args.length<1){
System.out.println("你需要指定加密文件的名字!");
return;
}
try {
oa = new encrypt(args[0]);
oa.openFile();
oa.coding();
oa.closeFile();
System.out.println("文件加密成功!");
} catch (FileNotFoundException e) {
System.out.println("没有找到文件:"+args[0]);
}catch (IOException e){
System.out.println("文件读写错误:"+args[0]);
}

}

}


12.Java序列化技术

用序列化来存储对象。

先定义一个用来序列化的类:

package io.xuliehua;
import java.io.*;
public class Student implements Serializable{
private String name;
private String ID;
private int age;
private String specialty;
public Student(String name, String ID, int age, String specialty){
this.name=name;
this.ID=ID;
this.age=age;
this.specialty=specialty;
}

public Student(){}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}

public String toString(){
return "姓名:"+name+"  学号:"+ID+"  年龄:"+age+"  专业:"+specialty;
}
}


package io.xuliehua;
import java.io.*;
public class ObjectFileTest {

public static void main(String[] args) {
Student st = new Student("周勇","20140101",18,"计算机");
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("save.dat"));
out.writeObject(st);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("save.dat"));
Student pupil = (Student)in.readObject();
in.close();
System.out.println(pupil);
System.out.println("姓名:"+pupil.getName());
System.out.println("学号:"+pupil.getID());
System.out.println("年龄:"+pupil.getAge());
System.out.println("专业:"+pupil.getSpecialty());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


程序运行结果如下:

姓名:周勇  学号:20140101  年龄:18  专业:计算机
姓名:周勇
学号:20140101
年龄:18
专业:计算机
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: