导航:首页 > 编程语言 > javaio流编程题

javaio流编程题

发布时间:2024-04-18 00:59:53

1. java Io流练习题大佬帮忙做一下啊/* * 使用扫描器接收键盘输入模拟用户登陆注册 * 要

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.util.Map.Entry;
importjava.util.Iterator;
importjava.util.Properties;
importjava.util.Scanner;

publicclassIoTest{
staticScannersc=newScanner(System.in);
staticPropertiespro=newProperties();
staticFilefile=newFile("K:\IO测试\Bai\登陆.properties");
staticStringid=null,pass=null,num=null;
staticBufferedReaderbr;
staticBufferedWriterbw;

publicstaticvoidmain(String[]args)throwsIOException{
if(!file.exists()){
bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file)));
pro.store(bw,"ID/Pass");
}else{
br=newBufferedReader(newInputStreamReader(newFileInputStream(file)));
pro.load(br);
}
while(true){
System.out.println("1.注册 2.登陆 请选择...");
num=sc.nextLine();
System.out.println("输入ID:");
id=sc.nextLine();
System.out.println("输入密码:");
pass=sc.nextLine();
if(num.equals("1"))
reg(id,pass);
else
sig(id,pass);
}
}

privatestaticvoidreg(Stringd,Stringp)throwsIOException{//注册
if(pro.getProperty(d)!=null){
System.out.println("用户存在不能注册,请直接登陆");
return;
}
pro.setProperty(d,p);
bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file)));
pro.store(bw,"ID/Pass");
System.out.println("-->注册成功");
}

privatestaticvoidsig(Stringd,Stringp)throwsIOException{//登陆
booleanb=false;
for(Iterator<Entry<Object,Object>>it=pro.entrySet().iterator();it.hasNext();){
Stringkey=(String)it.next().getKey();
if(key.equals(d)){
b=true;
break;
}
}
if(b){
if(!pro.getProperty(d).equals(p))
System.out.println("密码错误");
else
System.out.println("登陆成功<----");
}else{
System.out.println("用户不存在,请去注册");
}
}
}

2. 请教两道java编程题,关于IO包的。

nio不能实现一行一行读,只能一块一块读或者一个字符一个字符读。
代码如下:

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class Main
{
public static void main(String[] args)
{
Main t = new Main();
t.ReadFlieByLine_IO("E:\\123.txt");
t.ReadFileByLine_NIO("E:\\123.txt");
}

public void ReadFlieByLine_IO(String Filename)
{
File file = new File(Filename);
BufferedReader reader = null;
try
{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null)
{ //显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
/** finally {
if (reader != null){
try {
reader.close();
}
catch (IOException e1) {
}
}
}
*/
}

private void ReadFileByLine_NIO(String Filename)
{
FileInputStream file = null;
FileChannel reader = null;
try
{
file = new FileInputStream(Filename);
reader = file.getChannel();
String tempString = null;
ByteBuffer bb = ByteBuffer.allocate((int)reader.size());
reader.read(bb);
bb.flip();
String fileContent= new String(bb.array());
System.out.println(fileContent);
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
/** finally {
if (reader != null){
try {
reader.close();
}
catch (IOException e1) {
}
}
}
*/
}
}

3. java中Io流问题

importjava.io.*;
publicclassUpTest{
publicstaticvoidmain(String[]args){
Filetxt=newFile("K:\IO测试\大小写转换\Test.txt");//目标源!
BufferedReaderbr=null;//读!
BufferedWriterbw=null;//写!
StringBuilderstb=newStringBuilder();//缓存!
try{
br=newBufferedReader(newFileReader(txt));
try{
for(Stringstr=br.readLine();str!=null;str=br.readLine()){
char[]chs=str.toCharArray();//临时数组!
for(inti=0;i<chs.length;i++){//遍历!
if(Character.isUpperCase(chs[i])){//大小写转换!
chs[i]=Character.toLowerCase(chs[i]);
stb.append(chs[i]);
}else{
chs[i]=Character.toUpperCase(chs[i]);
stb.append(chs[i]);
}
}
stb.append(System.getProperty("line.separator"));//跨平台换行!
}
bw=newBufferedWriter(newFileWriter("K:\IO测试\大小写转换\Test1.txt"));//目标目的地!
bw.write(stb.toString());//写!
bw.flush();//刷新流!
}catch(IOExceptione){
e.printStackTrace();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}finally{//关流!
if(br!=null){
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(bw!=null){
try{
bw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
}
}

4. java 实现写文件功能 io流题


importjava.io.Console;
importjava.io.File;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;

publicclassUser{
privateStringuserName;
privateStringuserPass;

publicUser(StringuserName,StringuserPass){
super();
this.userName=userName;
this.userPass=userPass;
}

publicStringgetUserName(){
returnuserName;
}

publicvoidsetUserName(StringuserName){
this.userName=userName;
}

publicStringgetUserPass(){
returnuserPass;
}

publicvoidsetUserPass(StringuserPass){
this.userPass=userPass;
}

publicstaticvoidmain(String[]args)throwsIOException{
StringuserName=null;
StringuserPass=null;

List<User>userList=newArrayList<User>();

// Consoleconsole=System.console();
// for(inti=0;i<5;i++){
// userName=console.readLine("用户名:");
// userPass=console.readLine("密码:");
// Useruser=newUser(userName,userPass);
// userList.add(user);
// }
Scannerconsole=newScanner(System.in);
for(inti=0;i<5;i++){
System.out.print("用户名:");
userName=console.nextLine();
System.out.print("密码:");
userPass=console.nextLine();
Useruser=newUser(userName,userPass);
userList.add(user);
}
console.close();

Filefile=newFile("users.txt");
PrintWriterout=newPrintWriter(file,"UTF-8");
for(Useruser:userList){
out.println(String.format("用户名:%s,密码:%s",user.getUserName(),user.getUserPass()));
}
out.close();
}
}
用户名:123,密码:333
用户名:213,密码:333
用户名:asds,密码:123
用户名:sadas,密码:123
用户名:dasdas,密码:123

5. Java 编程基础题:io流和集合综合相关的题目不会做,哪位大神来看一下

此需求的关键点主要是两个:

1、IO读取文本信息;

2、TreeSet的排序问题。

解决过程如下:

首先通过TreeSet(Comparator<? super E>comparator) 构造方法指定TreeSet的比较器进行排序,而创建自己的实现比较器。

packagetest.treeset;
importjava.util.Comparator;
<Student>{
@Override
publicintcompare(Studentstu1,Studentstu2){
if(stu1.getScore()>stu2.getScore()){
return-1;
}elseif(stu1.getScore()==stu2.getScore()){
if(stu1.getAge()>stu2.getAge()){
return-1;
}elseif(stu1.getAge()==stu2.getAge()){
return0;
}else{
return1;
}
}else{
return1;
}
}
}

创建学生Bean类

packagetest.treeset;
importjava.io.Serializable;
{
privateStringname;
privateintage;
privateintscore;
publicStudent(Stringname,intage,intscore){
this.name=name;
this.age=age;
this.score=score;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicintgetScore(){
returnscore;
}
publicvoidsetScore(intscore){
this.score=score;
}
@Override
publicStringtoString(){
returnname+"-"+age+"-"+score;
}
}

创建实现类,实现对文件的读取已经将读取内容实例化入Student中,添加入TreeSet中排序,之后顺序打印TreeSet数据。

packagetest.treeset;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.util.TreeSet;
/**
*已知项目根目录下student_info.txt文件中有如下数据:(姓名-年龄-总分)
*lucy-28-98
*lily-23-97
*robt-25-100
*wili-15-100
*klin-29-93搜索
*运用IO技术获取将该文件中的数据分别封装成5个Student(姓名为String类型,年龄为int类型,总分为int类型)对象存入TreeSet集合中(需要自己定义Student类)
*要求:
*根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。
*按照排序完的顺序将所有信息打印到控制台上:
*打印格式如下:
*robt-25-100
*wili-15-100
*lucy-28-98
*lily-23-97
*klin-29-93
*/
publicclassIoReadTest{
publicstaticvoidmain(String[]args){
TreeSet<Student>set=newTreeSet<Student>(newMyComparator());
try{
FileInputStreamfis=newFileInputStream(newFile("D:\student_info.txt"));
InputStreamReaderisr=newInputStreamReader(fis,"UTF-8");
BufferedReaderbr=newBufferedReader(isr);
Stringstr="";
while((str=br.readLine())!=null){
String[]info=str.split("-");
Stringname=info[0];
intage=Integer.parseInt(info[1]);
intscore=Integer.parseInt(info[2]);
Studentstudent=newStudent(name,age,score);
set.add(student);
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
for(Studentstudent:set){
System.out.println(student);
}
}
}

运行后输出结果

robt-25-100
wili-15-100
lucy-28-98
lily-23-97
klin-29-93
阅读全文

与javaio流编程题相关的资料

热点内容
鱼竿裁布尺寸计算法 浏览:68
压缩机饮水机价格 浏览:281
win7linux切换 浏览:553
dos命令中提示拒绝访问 浏览:269
js调用java后台方法 浏览:309
我的世界命令教程 浏览:381
程序员生活中的自律习惯 浏览:161
java中编译与加载 浏览:566
正版秒赞源码官网 浏览:103
android云盘开发 浏览:964
139邮箱的发送服务器地址 浏览:826
长沙银行用什么样的APP 浏览:891
无忧推客源码 浏览:463
怎样用文件夹发微信上去 浏览:426
单片机movp1a什么意思 浏览:580
plc编程龙门铣床 浏览:375
毕业做程序员好吗 浏览:717
python最好的web框架 浏览:233
程序员同学玩腾讯 浏览:349
pdf扫描压缩 浏览:164