package File_test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test_caclmec {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public String getString(String info) throws IOException {
String temp = null;
System.out.println(info);
temp = br.readLine();
return temp;
}
public int getInt(String info,String err) throws IOException {
int temp = 0;
String str = null;
boolean flag = true;
while(flag) {
str = this.getString(info);//
if(str.matches("^\\d+$")) {//校验str是否是数字 ab
temp = Integer.parseInt(str);
flag = false;
}else {
System.out.println(err);
}
}
return temp;
}
public double getDouble(String info,String err) throws IOException {
double temp = 0;
String str = null;
boolean flag = true;
while(flag) {
str = this.getString(info);//
// if(str.matches("^\\d+.?\\d*$")) {//校验str是否是double数字
if(str.matches("^\\d+(\\.\\d+)?$")) {//校验str是否是double数字
temp = Double.parseDouble(str);
flag = false;
}else {
System.out.println(err);
}
}
return temp;
}
//扩展日期
public Date getDate(String info,String err) throws IOException, ParseException {
Date d = null;
String str = null;
boolean flag = true;
while (flag){
str = this.getString(info);
if(str.matches("^\\d{4}-\\d{2}-\\d{2}$")){//2019-08-05
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
d = sdf.parse(str);
flag = false;
}else{
System.out.println(err);
}
}
return d;
}
public static void main(String[] args) throws IOException, ParseException {
// int a = 0;
// int b = 0;
// double a1=0;
// double b1=0;
test_caclmec id = new test_caclmec();
// a1 = id.getDouble("请输入第一个数字:", " 输入的数字不合法,请重新输入...");
// b1 = id.getDouble("请输入第二个数字:", " 输入的数字不合法,请重新输入...");
// System.out.print(a1+"+"+b1+"=");
// System.out.printf("%.3f",(a1+b1));
Date date = id.getDate("请输入字符串","输入格式有误");
System.out.println(date);
}
}
上面为一种代码风格,每个功能模块化,便于阅读代码。
DataStream
package File_test;
import java.io.*;
public class TestDataStream {
/**
* 将表单中的数据保存到文件中:使用数据操作流
*/
// public static void main(String[] args) {
// File f = new File("d:"+File.separator+"000.txt");
// DataOutputStream dos = null;
// try {
// dos = new DataOutputStream(new FileOutputStream(f));
// String []name = {"123","456","789"};
// float [] price = {123.0f,456.5f,9554.f};
//
// int [] num = {2,5,95};
// dos.writeInt(name.length);
// for (int i = 0;i<name.length;i++){
// dos.writeChars(name[i]);
// dos.writeChar('\t');
// dos.writeFloat(price[i]);
// dos.writeChar('\t');
// dos.writeInt(num[i]);
// dos.writeChar('\n');
// }
// dos.close();
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
//
// }
public static void main(String[] args) throws IOException {
File f = new File("d:"+File.separator+"000.txt");
DataInputStream dis = new DataInputStream(new FileInputStream(f));
String name = null;
float price;
int num = 0;
char [] temp = null;
char c =0;
int n;
int t =0;
int qwe;
// qwe = dis.read();
qwe = dis.readInt();
while (qwe>0){
temp = new char[128];
n = 0;
while ((c=dis.readChar())!='\t'){
temp[n] = c;
n++;
}
name = new String(temp,0,n);
price = dis.readFloat();
dis.readChar();
num = dis.readInt();
dis.readChar();
System.out.println(name+'\t'+price+'\t'+num);
qwe--;
}
dis.close();
}
}
----------------------压缩流--------------------------------------------------------
package File_test;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* 压缩流:可以节省流量,节省时间,方便信息的传输
*
**/
public class Test_Zip {
public static void main(String[] args) throws IOException {
File file = new File("d:"+File.separator+"stu.txt");
File zipFile = new File("d:"+File.separator+"0.zip");
InputStream is = new FileInputStream(file);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
zos.putNextEntry(new ZipEntry(file.getName()));
zos.setComment("123456");
int temp = 0;
while ((temp=is.read())!=-1){
zos.write(temp);
}
is.close();
zos.close();
}
}
*回退流: -----------------------------------------------------------
package File_test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
/**
* 回退流:如果不希望某个内容被读取,
* 要是通过程序解决的话,
* 就需要用到回退流(针对输入流)
*/
public class TestBack {
public static void main(String[] args) throws IOException {
String str = "abcdefghijklmnopqrstuvwxyz";
byte b [] = str.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
PushbackInputStream pis = new PushbackInputStream(bais);
System.out.println("使用回退流之后:");
int temp = 0;
while ((temp = pis.read())!=-1){
if(temp=='l'){
pis.unread(temp);//回退
temp = pis.read();
}else {
System.out.print((char)temp);
}
}
}
}
序列化:将对象(对象的属性)变成二进制流的过程
反序列化:将二进制流重新生成对象的过程
用来方便的实现对象的存储或传输
实现Serializable接口
package File_test;
import java.io.Serializable;
public class testserializableperson implements Serializable {
//static final long serialVersionUID = 42L;
// private transient int asdas;加上之后该属性就不能被序列化
private String name;
private int age;
public testserializableperson() {
}
public String toString() {
return "testserializableperson{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/* public static long getSerialVersionUID() {
return serialVersionUID;
}*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public testserializableperson(String name, int age) {
this.name = name;
this.age = age;
}
}
---------------------------测试类----------------------------------------
package File_test;
import java.io.*;
public class TestSerializable {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://person.txt"));
testserializableperson p = new testserializableperson("lkzmj",19);
oos.writeObject(p);
System.out.println(p);
String a = "546";
a.hashCode();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d://person.txt"));
// testserializableperson tp = (testserializableperson) ois.readObject();
Object ooo = ois.readObject();
System.out.println(ooo);
oos.close();
ois.close();
}
}