导航:首页 > 编程语言 > 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编程题相关的资料

热点内容
百姓网app截图是什么意思 浏览:222
php如何嵌入html 浏览:809
解压专家怎么传输 浏览:743
如何共享服务器的网络连接 浏览:132
程序员简易表白代码 浏览:166
什么是无线加密狗 浏览:62
国家反诈中心app为什么会弹出 浏览:67
cad压缩图打印 浏览:102
网页打开速度与服务器有什么关系 浏览:863
android开发技术文档 浏览:64
32单片机写程序 浏览:49
三星双清无命令 浏览:837
汉寿小程序源码 浏览:344
易助erp云服务器 浏览:532
修改本地账户管理员文件夹 浏览:418
python爬虫工程师招聘 浏览:285
小鹏p7听音乐哪个app好 浏览:357
linux下的防火墙 浏览:964
凌达压缩机美芝压缩机 浏览:353
php后面代码不执行 浏览:238