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) {
}
}
- java学习总结——第十六天
- JAVA学习第十六天
- Java学习第十六天
- Core Java第十六章知识点总结——反射
- Java学习总结第十六天Java类与对象笔试题(一)
- Java学习总结之第十六章 Socket用法详解
- MySQL快速入门(二)——Java进阶学习第十六天
- No_16_0311 Java基础学习第十六天
- 第十六天学习java文件的操作
- 第十六周——学期总结
- 第十六天:总结
- 第十六天总结0411
- 第十六天总结
- 连续总结第十六天
- 第十六天总结
- apue学习第十六天——信号(第十章)
- python学习第十六章——测试
- .net学习第十六天
- Linux之动态库的创建和使用
- CF 349B - Color the Fence
- 浅谈 struts2 之 chain
- windows平台c语言使用socket通信传送图片
- Win7中的快捷键
- java学习总结——第十六天
- 安卓 入门扫盲网络资料
- 日志-fcntl
- 2014Unity亚洲开发者大会会议简录之技术篇
- BaseActivity类
- Repeating Table Header on Each Page of the Report in the SSRS 2008
- 位运算与和异或求平均值
- AsyncTask浅析
- bump-the-pointer VS TLABs