一个IO小问题,,求大神解

LcShadow 2015-04-17 03:45:18
package com.shijian15_9_3;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Test {
public static void main(String arg[]){
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader bir = new BufferedReader(ir);
while(true){
try {
FileOutputStream fo = new FileOutputStream("User.txt");
BufferedOutputStream bo = new BufferedOutputStream(fo);
String name;
byte bname[] = new byte[30];
byte bpassword[] = new byte[30];
System.out.print("用户名:");
name = bir.readLine();
bname = name.getBytes();
if(name.equalsIgnoreCase("done"))
break;
fo.write(bname);
fo.flush();
String password;
System.out.print("密码:");
password = bir.readLine();
bpassword = password.getBytes();
fo.write(bpassword);
fo.flush();
fo.close();
} catch (Exception e) {
// TODO: handle exception
}
}

}

}


为什么不能写到文件里面????
...全文
233 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wgd81685 2015-04-21
  • 打赏
  • 举报
回复
引用 4 楼 lclovehlm 的回复:
[quote=引用 1 楼 wgd81685 的回复:] FileOutputStream fo = new FileOutputStream("User.txt"); 这句代码放到while(true)之前执行
为甚么你的可以哈》???求指导[/quote] FileOutputStream fo = new FileOutputStream("User.txt");这句代码的意思是寻找User.txt文件,如果User.txt不存在就创建User.txt文件,如果存在的话会清空User.txt中的内容 你之前的代码把FileOutputStream fo = new FileOutputStream("User.txt");放在while循环内部,导致你每次写的内容都被清除,就出现了你看到的写不进去的情况。实际上你的内容已经写入文件,只是又被清除了。
冥王之锤 2015-04-21
  • 打赏
  • 举报
回复
引用 8 楼 lclovehlm 的回复:
[quote=引用 7 楼 t_jl1979 的回复:] [quote=引用 6 楼 t_jl1979 的回复:] [quote=引用 5 楼 lclovehlm 的回复:] 如果你输入的是done程序才退出的哈。。。。不输入的话就不会退出。但就是写不进去
不能这么break,会导致fo.close执行不到,也就是一个io一直处于open状态,后面再对此文件的操作,哪怕程序是正确的,也写不进去。[/quote] FileOutputStream fo = new FileOutputStream("User1.txt", true); 这句后要加一个true,表示append模式,否则每次openfile,都会把文件内容清空。[/quote] 我在break前面加了fo.close但还是写不进去。。。[/quote]
package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) throws FileNotFoundException {
		BufferedReader bir = new BufferedReader(new InputStreamReader(System.in));
		FileOutputStream fo = new FileOutputStream("Userx.txt", true);
		try {
			while (true) {
				System.out.print("用户名:");
				String name = bir.readLine();
				if (name.equalsIgnoreCase("done"))
					break;
				fo.write(name.getBytes());
				System.out.print("密码:");
				String password = bir.readLine();
				fo.write(password.getBytes());
				fo.flush();
			}
		} catch (Exception e) {
		} finally {
			try {
				if (fo != null)
					fo.close();
			} catch (Exception e2) {
			}
		}
	}

}
这是改好的,你按这个再改为你需的。 重启下你的机器,错误的程序把user1.txt文件锁定 了。
冥王之锤 2015-04-20
  • 打赏
  • 举报
回复
引用 6 楼 t_jl1979 的回复:
[quote=引用 5 楼 lclovehlm 的回复:] [quote=引用 2 楼 t_jl1979 的回复:] if(name.equalsIgnoreCase("done")) break; break直接跳出了,后面都没执行。

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) {
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader bir = new BufferedReader(ir);
		try (FileOutputStream fo = new FileOutputStream("User.txt")) {
			System.out.print("用户名:");
			fo.write(bir.readLine().getBytes());

			System.out.print("密码:");
			fo.write(bir.readLine().getBytes());
			fo.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
如果你输入的是done程序才退出的哈。。。。不输入的话就不会退出。但就是写不进去[/quote] 不能这么break,会导致fo.close执行不到,也就是一个io一直处于open状态,后面再对此文件的操作,哪怕程序是正确的,也写不进去。[/quote] FileOutputStream fo = new FileOutputStream("User1.txt", true); 这句后要加一个true,表示append模式,否则每次openfile,都会把文件内容清空。
冥王之锤 2015-04-20
  • 打赏
  • 举报
回复
引用 5 楼 lclovehlm 的回复:
[quote=引用 2 楼 t_jl1979 的回复:] if(name.equalsIgnoreCase("done")) break; break直接跳出了,后面都没执行。

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) {
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader bir = new BufferedReader(ir);
		try (FileOutputStream fo = new FileOutputStream("User.txt")) {
			System.out.print("用户名:");
			fo.write(bir.readLine().getBytes());

			System.out.print("密码:");
			fo.write(bir.readLine().getBytes());
			fo.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
如果你输入的是done程序才退出的哈。。。。不输入的话就不会退出。但就是写不进去[/quote] 不能这么break,会导致fo.close执行不到,也就是一个io一直处于open状态,后面再对此文件的操作,哪怕程序是正确的,也写不进去。
LcShadow 2015-04-20
  • 打赏
  • 举报
回复
引用 2 楼 t_jl1979 的回复:
if(name.equalsIgnoreCase("done")) break; break直接跳出了,后面都没执行。

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) {
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader bir = new BufferedReader(ir);
		try (FileOutputStream fo = new FileOutputStream("User.txt")) {
			System.out.print("用户名:");
			fo.write(bir.readLine().getBytes());

			System.out.print("密码:");
			fo.write(bir.readLine().getBytes());
			fo.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
如果你输入的是done程序才退出的哈。。。。不输入的话就不会退出。但就是写不进去
LcShadow 2015-04-20
  • 打赏
  • 举报
回复
引用 1 楼 wgd81685 的回复:
FileOutputStream fo = new FileOutputStream("User.txt"); 这句代码放到while(true)之前执行
为甚么你的可以哈》???求指导
LcShadow 2015-04-20
  • 打赏
  • 举报
回复
引用 楼主 lclovehlm 的回复:
package com.shijian15_9_3; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Test { public static void main(String arg[]){ InputStreamReader ir = new InputStreamReader(System.in); BufferedReader bir = new BufferedReader(ir); while(true){ try { FileOutputStream fo = new FileOutputStream("User.txt"); BufferedOutputStream bo = new BufferedOutputStream(fo); String name; byte bname[] = new byte[30]; byte bpassword[] = new byte[30]; System.out.print("用户名:"); name = bir.readLine(); bname = name.getBytes(); if(name.equalsIgnoreCase("done")) break; fo.write(bname); fo.flush(); String password; System.out.print("密码:"); password = bir.readLine(); bpassword = password.getBytes(); fo.write(bpassword); fo.flush(); fo.close(); } catch (Exception e) { // TODO: handle exception } } } } 为什么不能写到文件里面????
为什么你的可以存到文件里??
LcShadow 2015-04-20
  • 打赏
  • 举报
回复
引用 7 楼 t_jl1979 的回复:
[quote=引用 6 楼 t_jl1979 的回复:] [quote=引用 5 楼 lclovehlm 的回复:] [quote=引用 2 楼 t_jl1979 的回复:] if(name.equalsIgnoreCase("done")) break; break直接跳出了,后面都没执行。

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) {
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader bir = new BufferedReader(ir);
		try (FileOutputStream fo = new FileOutputStream("User.txt")) {
			System.out.print("用户名:");
			fo.write(bir.readLine().getBytes());

			System.out.print("密码:");
			fo.write(bir.readLine().getBytes());
			fo.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
如果你输入的是done程序才退出的哈。。。。不输入的话就不会退出。但就是写不进去[/quote] 不能这么break,会导致fo.close执行不到,也就是一个io一直处于open状态,后面再对此文件的操作,哪怕程序是正确的,也写不进去。[/quote] FileOutputStream fo = new FileOutputStream("User1.txt", true); 这句后要加一个true,表示append模式,否则每次openfile,都会把文件内容清空。[/quote] 我在break前面加了fo.close但还是写不进去。。。
冥王之锤 2015-04-17
  • 打赏
  • 举报
回复
if(name.equalsIgnoreCase("done")) break; break直接跳出了,后面都没执行。

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Test {
	public static void main(String arg[]) {
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader bir = new BufferedReader(ir);
		try (FileOutputStream fo = new FileOutputStream("User.txt")) {
			System.out.print("用户名:");
			fo.write(bir.readLine().getBytes());

			System.out.print("密码:");
			fo.write(bir.readLine().getBytes());
			fo.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
wgd81685 2015-04-17
  • 打赏
  • 举报
回复
FileOutputStream fo = new FileOutputStream("User.txt"); 这句代码放到while(true)之前执行

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧