導航:首頁 > 編程語言 > 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流編程題相關的資料

熱點內容
單片機的材料 瀏覽:162
泰國大尺度 瀏覽:347
程序員的能力怎麼提高 瀏覽:195
你能告訴我電影院在哪裡嗎翻譯 瀏覽:554
java大數據方向 瀏覽:462
蛇電影洗澡 瀏覽:877
誰有在線免費觀看鏈接 瀏覽:568
編譯qemu520 瀏覽:987
idea忽略編譯指定的類 瀏覽:485
有一部電影主角是彈吉他的 瀏覽:761
主角是種馬穿越到動漫小說 瀏覽:392
免費看電影無廣告,無彈窗的網 瀏覽:610
sublimephp錯誤提示 瀏覽:913
螢石雲伺服器安裝路徑 瀏覽:643
pm3讀國產加密卡 瀏覽:507
解放戰爭片老電影完整版 瀏覽:989
提取應用app安裝包什麼意思 瀏覽:843
公司伺服器地址怎麼看 瀏覽:678
華融證券app在哪裡申購新債 瀏覽:556