java学习总结——第十六天

来源:互联网 发布:乌鸦森林之谜mac版 编辑:程序博客网 时间:2024/06/05 15:48

今天学习IO流的基本操作

字节流 

InputStream   --输入

import java.io.FileInputStream;
import java.io.IOException;


public class InputStream {


public static void main(String[] args) throws IOException {

String file = "d:\\123.txt";
FileInputStreamf = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while((len=f.read(bytes))!=-1 ){ //只读一次
System.out.println(new String(bytes,0,len));
}
// int len=f.read(bytes); //读取多少字节返回多少长度
//
// String s = new String(bytes,0,len);
// System.out.println(s);

// while (true) {
// int i  = f.read();
// if(i==-1){
// break;
// }
// System.out.print((char)i);
// }

f.close();
}
}

OutputStream--输出



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class OutputStream {
public static void main(String[] args) throws IOException {
String s = "d:/123.txt";
String s1 = "d:/123(副本).txt";
FileInputStream fis = new FileInputStream(s);
FileOutputStream fos1 = new FileOutputStream(s);
FileOutputStream fos = new FileOutputStream(s1);


fos1.write("姓名".getBytes());
fos1.write(13); // 换行 或者("\r\n")
fos1.write(10);
fos1.write("上学堂".getBytes());


byte[] arr = new byte[1024];
int len = 0;
while ((len = fis.read(arr)) != -1) {
fos.write(arr, 0, len);
fos.flush();
}
fos.close();
fos1.close();
fis.close();
System.out.println("Copy End !");
}
}

密码文件管理

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Writermima {
private static Scanner read;
String s = "123.txt";
public void login(){      //登录
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(s);
br = new BufferedReader(fr);
read = new Scanner(System.in);
System.out.print("login username:");
String name = read.nextLine();
System.out.print("login password:");
String password = read.nextLine();
int flag =0;
Map<String,String> map= new HashMap<String,String>();

// String pass = name + "=" + password;
// while(true){
//  if(pass.equalsIgnoreCase(br.readLine())){
// System.out.println("密码正确!");
// flag=1;break;
// }
// }
if(flag == 0){
System.out.println("登录失败,请重新输入");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}
public void register(){//注册
BufferedWriter bw = null;
FileWriter fw = null;

try {
fw = new FileWriter(s, true);
bw = new BufferedWriter(fw);

read = new Scanner(System.in);
System.out.print("register username:");
String name = read.nextLine();
System.out.print("register password:");
String password = read.nextLine();

bw.write(name+"="+password);
bw.newLine();
System.out.println("录入成功!");

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {


}
}


0 0
原创粉丝点击