導航:首頁 > 編程語言 > javaio編程題

javaio編程題

發布時間:2022-08-20 22:26:58

java io類題目

做過測試,以下為源代碼: try { FileInputStream fio=new FileInputStream("D:\\temp.txt"); ArrayList<String> strList=new ArrayList<String>(); ArrayList<Integer> intList=new ArrayList<Integer>(); byte[] bf=new byte[1000]; fio.read(bf); String str=new String(bf,"gb2312"); str=str.trim(); for(int i=0;i<str.length();i++){ char tempChar= str.charAt(i); boolean br=true; int j=0; for(;j<strList.size();j++){ if(strList.get(j).trim().equals((tempChar+"").trim())){ br=false; break; } } if(br){ strList.add(tempChar+""); intList.add(1); }else{ intList.set(j, intList.get(j)+1); } } for(int i=0;i<strList.size();i++){ System.out.println(strList.get(i)+"="+intList.get(i)); } } catch (Exception e) { e.printStackTrace(); } 推薦書籍:《Java編程思想》

⑵ Java關於io和多線程的編程題,求代碼

packageknow;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Scanner;
publicclassT20{
publicstaticvoidmain(String[]args)throwsIOException{
Scanners=newScanner(System.in);
Stringusername=null;
Stringpassword=null;
System.out.println("輸入用戶名:");
while(true){
if(username==null){
username=s.next();
System.out.println("輸入密碼:");
}else{
password=s.next();
}
if(password!=null){
s.close();
break;
}
}

BufferedReaderbr=null;
Map<String,String>map=newHashMap<String,String>();
try{
br=newBufferedReader(newInputStreamReader(newFileInputStream("d:/test.txt")));
Stringtemp=null;
while((temp=br.readLine())!=null){
String[]ss=temp.split("=");
map.put(ss[0],ss[1]);
}
}catch(IOExceptione){
throwe;
}finally{
if(br!=null)
br.close();
}
Stringu=map.get("userName");
Stringp=map.get("password");
if(u.equals(username)&&p.equals(password)){
System.out.println("登錄成功");
}else{
System.out.println("用戶名或密碼錯誤");
}
}
}

packageknow;
publicclassT21{
publicstaticvoidmain(String[]args)throwsInterruptedException{
String[]persons=newString[]{"甲","乙","丙","丁","午","己","庚","辛","壬","癸"};
for(inti=0;i<persons.length;i++){
System.out.println(persons[i]+"正在過山洞");
Thread.sleep(5000);
}
}
}

最後一個網路搜一個就行了,肯定比我畫的好

⑶ 求代碼,關於java io流的題目

按照你的要求編寫的文本瀏覽器的Java程序如下(這個程序比上一個程序的容錯能力更強)

//文本瀏覽器
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
{
JTextAreajta=newJTextArea(3,5);
JScrollPanejsp=newJScrollPane(jta);
JTextFieldjtf=newJTextField(20);
JButtonjb=newJButton("View");
JLabeljl=newJLabel("Filename");
JPaneljp=newJPanel();
P4(){
setTitle("文本瀏覽器");
jb.addActionListener(this);
jp.add(jl);jp.add(jtf);jp.add(jb);
add(jsp,BorderLayout.CENTER);
add(jp,BorderLayout.SOUTH);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==jb){
if(jtf.getText()==null||jtf.getText().equals("")){
JOptionPane.showMessageDialog(jtf,"請輸入文件名!");
jtf.requestFocus();
return;
}
BufferedReaderbr=null;
try{
StringfileName=jtf.getText().trim();
br=newBufferedReader(newFileReader(fileName));
Strings="";
StringBuffersb=newStringBuffer();
while((s=br.readLine())!=null){
sb.append(s+" ");
}
jta.setText(sb.toString());
}catch(FileNotFoundExceptionfnfe){
JOptionPane.showMessageDialog(jtf,"系統找不到指定文件!");
jtf.requestFocus();
}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{
if(br!=null)
br.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
publicstaticvoidmain(String[]args){
newP4();
}
}

運行結果

⑷ 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

⑸ java IO練習題

希望對你有用~~嘿嘿~~(我寫的是控制台的程序,沒用SWING做界面)
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class AbsenteeNote {
public static void main(String[] args) throws IOException {
int select = 0;
select = welcome();
if (select == 1 || select == 2) {
writeLetter(select);
} else {
viewLetter();
}
}

private static int welcome() {
int num = 0;//用於第一層菜單的選擇
int num1 = 0;//用於第二層菜單的選擇
do {
System.out.println("1:編寫請假條");
System.out.println("2:查看請假條");
System.out.print("請選擇功能號:");
Scanner in = new Scanner(System.in);
num = in.nextInt();
} while (num != 1 && num != 2);//若輸出的數字不為1或2,則繼續選擇
if (num == 1) {
do {
System.out.println("1:病假");
System.out.println("2:事假");
System.out.print("請選擇功能號:");
Scanner in = new Scanner(System.in);
num1 = in.nextInt();
} while (num1 != 1 && num1 != 2);
return num1;
} else {//當輸入的是2(查看請假條),則任意返回一個值。
return 0;
}
}

private static void viewLetter() {//查看請假條
File letter = new File("c:\\Letter\\Letter.txt");
try {
FileInputStream view = new FileInputStream(letter);
byte b[] = new byte[view.available()];

while (view.read(b) != -1) {
FilterOutputStream viewContain=new FilterOutputStream(System.out);
System.out.println("假條內容如下:");
viewContain.write(b);
viewContain.close();
}
view.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在!!!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private static void writeLetter(int num1) {//寫請假條
String str = new String();
if (num1 == 1)
str = "我因發燒,請假一天 ";
else
str = "參加婚禮,於4.10請假一天";
File letter = new File("c:\\Letter\\Letter.txt");
StringBuilder contain = new StringBuilder();
Scanner in = new Scanner(System.in);
System.out.print("寄信人地址:");
contain.append("寄信人地址:" + in.nextLine() + "\r\n");
System.out.print("收件人地址:");
contain.append("收件人地址:" + in.nextLine() + "\r\n");
System.out.print("簽名:");
contain.append("簽名:" + in.nextLine() + "\r\n");
System.out.print("日期:");
contain.append("日期:" + in.nextLine() + "\r\n");
contain.append(str);
byte[] input = contain.toString().getBytes();
try {
FileOutputStream out = new FileOutputStream(letter);
out.write(input, 0, input.length);
System.out.println("請假條生成成功!!!");
out.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在!!!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

⑹ JavaIO操作題

剛好有個以前寫的練習記錄,可能有參考作用://對象轉位元組數組publicstaticbyte[]GetBytes(Objectobj)throwsException{ ByteArrayOutputStreambos=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bos); oos.writeObject(obj); byte[]byteArray=bos.toByteArray(); returnbyteArray;}運行結果查看示例:提取碼:m3pr

⑺ 請教兩道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) {
}
}
}
*/
}
}

⑻ Java語言 編程題目

import java.io.File;

public class Folder {

/**
* @param args
*/
public static void main(String[] args) {
if (args.length == 0) {// 顯示當前目錄下的樹狀內容
showAllContent("./", "");
return;
}
if (args.length == 1) {// 顯示指定目錄下的內容
showAllContent(args[0], "");
return;
}
if (args.length == 2) {// 添加或刪除子目錄
File file = new File("./");// 用於保存當前目錄
File newFile = new File(file, args[1]);
if (args[0].equals("add")) {// 添加
if (newFile.isDirectory())
if (!newFile.exists()) {
newFile.mkdirs();
showAllContent("./", "");
} else
System.out.println("創建失敗,文件夾已經存在!");
else
System.out.println("創建失敗,創建項不是一個文件夾");
} else if (args[0].equals("del")) {// 刪除
if (newFile.isDirectory())
if (newFile.exists()) {
delDir(newFile);
showAllContent("./", "");
} else
System.out.println("刪除失敗,該文件夾不存在");
else {
System.out.println("刪除失敗,刪除項不是一個文件夾");
}
} else {
System.out.println("參數有誤!");
helpMessage();
}
} else {
System.out.println("參數有誤!");
helpMessage();
}

}

/*
* 顯示文件夾內容,包括子文件夾的所有內容
*/
public static void showAllContent(String fileName, String str) {
File file = new File(fileName);
for (int i = 0; i < str.length(); i++) {
if ((i + 1) % 3 == 0)
System.out.print("|");
else
System.out.print(" ");
}
System.out.println("----" + file.getName());
str += "---";
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null)
for (int i = 0; i < files.length; i++) {
showAllContent(files[i].getPath(), str);
}
}
}

/*
* 刪除文件夾
*/
public static void delDir(File dir) {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File f : files) {
delDir(f);
}
}
dir.delete();
}
/*
* 幫助信息
*/
public static void helpMessage(){
System.out.println("1. 輸入: java Folder,顯示當前文件夾下的目錄和文件的文本形式樹狀顯示;");
System.out.println("2. 輸入: java Folder <folder name>,顯示<folder name>文件夾下的目錄和文件的文本形式樹狀顯示;(<folder name>可以為任意文件夾路徑名)");
System.out.println("3. 輸入: java Folder add <folder name>,在當前文件夾下面添加一個子目錄(子目錄名為:<folder name>),同時顯示當前文件夾下的最新目錄和文件的文本形式樹狀顯示;");
System.out.println("4. 輸入: java Folder del <folder name>,在當前文件夾下面刪除一個子目錄(子目錄名為:<folder name>),同時顯示當前文件夾下的最新目錄和文件的文本形式樹狀顯示;");
}

}

⑼ Java關於io和多線程的編程題

可以做個區域網聊天程序,兩個線程分別對應收信息和發信息,收發過程涉及io,tcp協議比較適合練習

閱讀全文

與javaio編程題相關的資料

熱點內容
度人經pdf 瀏覽:898
怎麼配置android遠程伺服器地址 瀏覽:956
java程序員看哪些書 瀏覽:939
什麼app可以免費和外國人聊天 瀏覽:793
pdf手寫筆 瀏覽:178
別永遠傷在童年pdf 瀏覽:986
愛上北斗星男友在哪個app上看 瀏覽:419
主力散戶派發源碼 瀏覽:669
linux如何修復伺服器時間 瀏覽:59
榮縣優途網約車app叫什麼 瀏覽:477
百姓網app截圖是什麼意思 瀏覽:226
php如何嵌入html 瀏覽:813
解壓專家怎麼傳輸 瀏覽:745
如何共享伺服器的網路連接 瀏覽:134
程序員簡易表白代碼 瀏覽:168
什麼是無線加密狗 瀏覽:64
國家反詐中心app為什麼會彈出 瀏覽:69
cad壓縮圖列印 瀏覽:104
網頁打開速度與伺服器有什麼關系 瀏覽:865
android開發技術文檔 瀏覽:65