下列java输出姓名学号代码为什么运行后,不管学号输入什么都显示“该用户已存在”,无法判别是否真的存在,如何修改?

怎样学号java_百度知道
怎样学号java
我有更好的答案
如果是在校学生的话可以自学 如果已经工作了
想要学好java 除非你有大毅力
java并不难
难的是你有没有那个毅力 和 学习氛围
要是没学习氛围
估计也白搭你可以报培训班
好的培训班的学习氛围很好
可以敦促你学习。当然码农危险
入行请谨慎考虑
码农什么意思
码农 就是苦逼程序员的称呼 敲代码的 农民兄弟
程序员的一个特质是 可以自己通过 GOOGLE和Baidu来解决问题
怎么这么了解
码农一枚,所以了解苦逼,结贴吧
你Q多少,交流一下吧
采纳率:68%
上学校啊学习啊
自学怎么搞
自学的话就是卖书,去论坛、qq群啊之类的,现在网上资源很多啊。
能不能介绍个网址
为您推荐:
其他类似问题
您可能关注的内容
java的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。JAVA按要求完成下列程序编写,学生类_百度知道
JAVA按要求完成下列程序编写,学生类
⒈创建一个学生类,数据成员包含了学号、姓名、数学成绩,方法包含了构造方法、输出学生信息的方法、修改成绩的方法、获得成绩的方法。
⒉建立一个含有n个学生的数组,n由用户从键盘输入,每个学生的信息也让用户从键盘输入,计算学生的数学成绩的平均分。
前面三个条件我基本上都能够达成,主要是加入后面两个条件就不会做了T.T 不知道要怎么修改
我有更好的答案
package&import&java.util.Spublic&class&Student&implements&Compare{ private&String& private&String& private&double& public&Student&(&String&no,&String&name,&double&score&) {
this.no&=&
this.name&=&
this.score&=& } public&String&displays&() {
StringBuilder&builder&=&new&StringBuilder&();
builder.append&(&Student&[no=&);
builder.append&(no);
builder.append&(&,&name=&);
builder.append&(name);
builder.append&(&,&score=&);
builder.append&(score);
builder.append&(&]&);
return&builder.toString&(); } public&void&setScore&(&double&score&) {
this.score&=& } public&double&getScore&() {
return& } @Override public&boolean&compare&(&Object&o&) {
return&(&this&==&o&||&null&!=&o&&&&o.getClass&().equals&(this.getClass&())&)
&&&(&(Student)&o&).score&&&this. } public&static&void&sort&(&Student[]&students&) {
for&(&int&i&=&0;&i&&&students.&i++&)
for&(&int&j&=&i&+&1;&j&&&students.&j++&)
if&(students[i].compare&(students[j]))
Student&temp&=&students[i];
students[i]&=&students[j];
students[j]&=&
} } public&static&void&main&(&String[]&args&) {
System.out.print&(&建立一个含有n个学生的数组,&从键盘输入n:&&);
Scanner&scanner&=&new&Scanner&(System.in);
int&count&=&0;
double&scores&=&0;
int&n&=&scanner.nextInt&();
Student[]&students&=&new&Student[n];
System.out.println&(&从键盘输入每个学生的信息:&学号、姓名、数学成绩&用英文逗号分隔.&);
String&line&=&
while&(count&&&n&&&&null&!=&(&line&=&scanner.next&()&))
if&(line.matches&(&^\\w+\\,\\w+\\,\\d+(\\.\\d*)?$&))
String&no&=&line.split&(&\\,&)[0].trim&();
String&name&=&line.split&(&\\,&)[1].trim&();
double&score&=&Double.parseDouble&(line.split&(&\\,&)[2].trim&());
students[count]&=&new&Student&(no,&name,&score);
scores&+=&
catch&(ScoreException&e)
e.printStackTrace&();
System.out.println&(&排序:&);
sort&(students);
for&(&int&i&=&0;&i&&&students.&i++&)
System.out.println&(students[i].displays&());
System.out.println&(&学生的数学成绩的平均分:&&&+&(&scores&/&n&));
scanner.close&();
Student&collegeStudent&=&new&CollegeStudent&(&001&,&&yugi111&,&120,&&&,&&man&);
System.out.println&(collegeStudent.displays&()); }}class&CollegeStudent&extends&Student{ private&String&D private&String& public&CollegeStudent&(&String&no,&String&name,&double&score,&String&Date,&String&sex&) {
super&(no,&name,&score);
this.Date&=&D
this.sex&=& } @Override public&String&displays&() {
StringBuilder&builder&=&new&StringBuilder&();
builder.append&(&CollegeStudent&[Date=&).append&(Date).append&(&,&sex=&).append&(sex).append&(&]&);
return&builder.toString&(); }}interface&Compare{ boolean&compare&(&Object&o&);}class&ScoreException&extends&IllegalArgumentException{ private&static&final&long&serialVersionUID&=&1L; public&ScoreException&() {
super&(); } public&ScoreException&(&String&s&) {
super&(s); } static&ScoreException&forInputString&(&String&s&) {
return&new&ScoreException&(&For&input&string:&\&&&+&s&+&&\&&); }}
谢谢,你的答案很完整,虽然我看了很久还有些地方没看懂的…比如74行到第92行之间的…原谅我JAVA几乎才刚入门,不太理解…
额,说来话长啊
采纳率:71%
public class Student implements Comparable&Student& { private S// 学号 private S// 姓名 private int mathS // 数学成绩public Student() {
// TODO Auto-generated constructor stub }public String getNumber() {
}public void setNumber(String number) {
this.number = }public String getName() {
}public void setName(String name) {
this.name = }public int getMathScore() {
return mathS }public void setMathScore(int mathScore) {
this.mathScore = mathS }@Override public String toString() {
return &Student [数学成绩=& + mathScore + &, 姓名=& + name
+ &, 学号=& + number + &]&; }@Override public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((number == null) ? 0 : number.hashCode());
}@Override public boolean equals(Object obj) {
if (this == obj)
if (obj == null)
if (getClass() != obj.getClass())
Student other = (Student)
if (number == null) {
if (other.number != null)
} else if (!number.equals(other.number))
} @Override public int compareTo(Student student) {
if(student == null)
return this.getMathScore()-student.getMathScore(); }}
给你一个相对完整的代码吧Student类:package&com.baidu.import&java.util.Limport&java.util.Spublic&class&Student&implements&Comparable&Student&&{&//&继承现有的java.lang.Comparable接口,可支持List的排序 private&String&stuNo;&//&学号 private&String&stuN&//&姓名 private&float&mathS&//&数学成绩
public&Student()&{}&//&默认构造方法
public&Student(String&stuNo,&String&stuName,&float&mathScore)&throws&ScoreException&{&//&初始化构造方法
this.stuNo&=&stuNo;
this.stuName&=&stuN
//&这就是你要的在初始化时判断成绩是否合理
if(mathScore&&&0&||&mathScore&&&100)&{
throw&new&ScoreException(&分数不能小于0且不能大于100&);
if(mathScore&%&0.5f&!=&0)&{
throw&new&ScoreException(&分数必须是整数或是0.5的倍数&);
this.mathScore&=&mathS } public&void&disylay()&{&//&输出成绩
//&以类似表格的形式输出
System.out.println(String.format(&%s\t%s\t%.1f&,&stuNo,&stuName,&mathScore)); }
public&String&getStuNo()&{
return&stuNo; } public&void&setStuNo(String&stuNo)&{
this.stuNo&=&stuNo; } public&String&getStuName()&{
return&stuN } public&void&setStuName(String&stuName)&{
this.stuName&=&stuN }
//&获得成绩的方法直接用Getter方法 public&float&getMathScore()&{
return&mathS } //&修改成绩的方法也直接用Setter方法 public&void&setMathScore(float&mathScore)&{
this.mathScore&=&mathS } /** &*&创建学生对象 &*&@param&scanner &*&@return &*/ public&static&Student&create(Scanner&scanner)&{
String&stuNo;
String&stuN
float&mathS
System.out.print(&请输入学号:&);
stuNo&=&scanner.next();
System.out.print(&请输入姓名:&);
stuName&=&scanner.next();
do&{&//&获取数学成绩,当成绩不合理时重新输入,进入无限循环
mathScore&=&inputScore(scanner);
//若学生成绩合理即退出无限循环
student&=&new&Student(stuNo,&stuName,&mathScore);
}&catch&(ScoreException&e)&{
System.out.println(e.getMessage());
}&while(true);
private&static&float&inputScore(Scanner&scanner)&{
System.out.print(&请输入成绩:&);
float&mathS
mathScore&=&scanner.nextFloat();
return&mathS } /** &*&计算平均分 &*&@param&students &*&@return &*/ public&static&float&getAvg(List&Student&&students)&{
if(null&==&students&||&students.isEmpty())&{
int&totalScore&=&0;
for(Student&s&:&students)&{
totalScore&+=&s.getMathScore();
return&(float)&(totalScore&/&(double)&students.size()); } @Override public&int&compareTo(Student&o)&{
if(null&==&o)&{
if(this.mathScore&&&o.mathScore)&{
return&-1;
}&else&if(this.mathScore&&&o.mathScore)&{
return&0; }}CollegeStudent类:package&com.baidu.import&java.text.DateFimport&java.text.ParseEimport&java.text.SimpleDateFimport&java.util.Cimport&java.util.Dimport&java.util.Spublic&class&CollegeStudent&extends&Student&{ private&Date&birthD private&String&
private&DateFormat&outFormat&=&new&SimpleDateFormat(&yyyy-mm-dd&);&//&输出的日期格式 private&static&DateFormat&inFormat&=&new&SimpleDateFormat(&yyyymmdd&);&//&输入的日期格式
private&CollegeStudent()&{
super(); } private&CollegeStudent(String&stuNo,&String&stuName,&float&mathScore)&throws&ScoreException&{
super(stuNo,&stuName,&mathScore);
Calendar&cal&=&Calendar.getInstance();
int&year&=&cal.get(Calendar.YEAR)&-&20;&//&出生日期预设为20年前
cal.set(Calendar.YEAR,&year);
this.birthDate&=&cal.getTime();
this.sex&=&&未知&; }
public&Date&getBirthDate()&{
return&birthD } public&void&setBirthDate(Date&birthDate)&{
this.birthDate&=&birthD }
public&String&getSex()&{
return& } public&void&setSex(String&sex)&{
this.sex&=& }
@Override public&void&disylay()&{
System.out.println(String.format(&%s\t%s\t%.1f\t%s\t%s&,&getStuNo(),
getStuName(),&getMathScore(),
outFormat.format(this.birthDate),&this.sex)); } /** &*&创建学生对象 &*&@param&scanner &*&@return &*/ public&static&Student&create(Scanner&scanner)&{&//&静态方法不可继承
String&stuNo;
String&stuN
float&mathS
System.out.print(&请输入学号:&);
stuNo&=&scanner.next();
System.out.print(&请输入姓名:&);
stuName&=&scanner.next();
CollegeStudent&
do&{&//&获取数学成绩,当成绩不合理时重新输入,进入无限循环
mathScore&=&inputScore(scanner);
//若学生成绩合理即退出无限循环
student&=&new&CollegeStudent(stuNo,&stuName,&mathScore);
}&catch&(ScoreException&e)&{
System.out.println(e.getMessage());//
mathScore&=&inputScore(scanner);
}&while(true);
System.out.print(&请输入出生日期(如):&);
String&birth&=&scanner.next();
Date&birthDate&=&
birthDate&=&inFormat.parse(birth);
}&catch&(ParseException&e)&{
if(null&!=&birthDate)&{
student.setBirthDate(birthDate);
System.out.print(&请输入性别(0:男,1或其他:女):&);
int&gender&=&scanner.nextInt();
String&sex&=&
if(0&==&gender)&{
sex&=&&男&;
sex&=&&女&;
if(null&!=&sex)&{
student.setSex(sex);
//&返回类型为Student,即是向上转型
private&static&float&inputScore(Scanner&scanner)&{
System.out.print(&请输入成绩:&);
float&mathS
mathScore&=&scanner.nextFloat();
return&mathS }}异常类:package&com.baidu.public&class&ScoreException&extends&Exception&{ private&static&final&long&serialVersionUID&=&1L; public&ScoreException()&{
super(); } public&ScoreException(String&arg0)&{
super(arg0); }}测试类:package&com.baidu.import&java.util.ArrayLimport&java.util.Cimport&java.util.Limport&java.util.S/**&*&测试类&*/public&class&TestStudent&{ static&int&n;&//&学生数量 static&List&Student&&students&=&new&ArrayList&Student&();&//&学生数组,用List方便排序 private&static&Scanner& public&static&void&main(String[]&args)&{
scanner&=&new&Scanner(System.in);&//&初始化键盘输入
setN(false);
inputStudents();
showStudents(); }
//&循环输入学生信息 private&static&void&inputStudents()&{
for(int&i&=&0;&i&&&n;&i&++)&{
System.out.println(String.format(&请输入第%d个学生信息:&,&i&+&1));
Student&s&=&
//&交替创建Student对象和CollegeStudent对象
if(i&%&2&==&0)&{
s&=&Student.create(scanner);
s&=&CollegeStudent.create(scanner);
if(null&!=&s)&{
students.add(s);
System.out.println(&------------------------------------------------------&);
Collections.sort(students);&//&升序排序,即分数从低到高
Collections.reverse(students);&//&反转列表,即将排序设置成降序 }
//&输出学生信息 private&static&void&showStudents()&{
System.out.println(&\r\n学生信息如下:\r\n学号\t姓名\t成绩\t出生日期\t\t性别&);
for(Student&s&:&students)&{
s.disylay();
System.out.println(&\r\n平均分:&&+&Student.getAvg(students)); } /** &*&迭代输入学生数量 &*&@param&retry&-&用来标识是否重新输入,以显示不同提示 &*/ private&static&void&setN(boolean&retry)&{
if(retry)&{
System.out.print(&请重新输入学生数量:&);
System.out.print(&请输入学生数量:&);
n&=&scanner.nextInt();
if(n&&&0)&{&//&判断n的有效性
System.out.println(&学生数量已设为&&+&n);
System.out.print(&学生数量必须大于0,&);
setN(true); }}
只给你写那么多,下面的自己查资料,多动脑,才能学好编程,希望采纳package com.public class Student {
private String sN
private String sN
private float sM
public Student(){}
* @param sNum
* @param sName
* @param sMath
* 有参构造方法
public Student(String sNum,String sName,float sMath){
this.sNum = sN
this.sName = sN
this.sMath = sM
* @param student
* 输出学生信息
public void printStudent(Student student){
System.out.println( &学号&+student.getsNum()+&
姓名:&+student.getsName()+&
数学成绩&+student.getsMath() );
}public String getsNum() {
public void setsNum(String sNum) {
this.sNum = sN
public String getsName() {
public void setsName(String sName) {
this.sName = sN
public float getsMath() {
* @param sMath
* 修改学生成绩
public void setsMath(float sMath) {
this.sMath = sM
}}package com.import java.util.Spublic class TestStudent {
public static void main(String[] args) {
@SuppressWarnings(&resource&)
Scanner sc = new Scanner(System.in);
//由键盘输入数组大小
Student[] array = new Student[ sc.nextInt() ];
for(int i=0;i&array.i++){
Student student = array[i];
//键盘输入学号
student.setsNum(sc.nextLine());
//键盘输入姓名
student.setsName(sc.nextLine());
//键盘输入数学成绩
student.setsMath(sc.nextFloat());
//计算数组里学生的平均成绩
float sum = 0;
float avgMath = 0;
for(int i=0;i&array.i++){
sum +=array[i].getsMath();
avgMath = sum/array.
System.out.println(&学生的平均成绩是:&+avgMath);
}}package com.import java.util.Dpublic class CollegeStudent extends Student{
private Date sD
private String sS
public CollegeStudent(){}
public CollegeStudent(Date sDate,String sSex){
this.sDate = sD
this.sSex = sS
/* (non-Javadoc)
* @see com.test.Student#printStudent(com.test.Student)
* 重写Student的方法,不知道你说的display()就是此方法,只不过名字不同
public void printStudent(CollegeStudent student){
System.out.println( &学号&+student.getsNum()+&
姓名:&+student.getsName()
数学成绩&+student.getsMath()+&出生日期:&+student.getsDate()+&性别:&+student.getsSex() );
public Date getsDate() {
public void setsDate(Date sDate) {
this.sDate = sD
public String getsSex() {
public void setsSex(String sSex) {
this.sSex = sS
嗯嗯,谢谢你,你的代码我有很认真的看,有些之前不懂的地方看完之后就懂了。
采纳吧,下面还要你自己努力,不难,辛苦一个小时
这么说吧,技术难为0,真是入门级的东西但是工作量很大,如果你真心想学java呢,建议还是自己多动手,你哪里不会你可以问但是要人完全把代码写给你,对你没一点好处,而且这个还得花不少时间
我有写,做到第三个的时候不懂了,不知道什么是建立上转型变量测试…
我有写,做到第三个的时候不懂了,不知道什么是建立上转型变量测试…
真心想帮你,但是这种入门级的代码没兴趣写!!
赞同1楼QQMSD8的说法,我写了第一个,你尝试着写写其他的吧:package&com.public&class&Student&{
private&String&S
private&String&
private&String&mathS
public&Student()&{ }
public&Student(String&sno,String&name,String&mathScore)&{
this.name=
this.mathScore=mathS } public&String&getSno()&{
return&S } public&void&setSno(String&sno)&{
Sno&=& } public&String&getName()&{
return& } public&void&setName(String&name)&{
this.name&=& } public&String&getMathScore()&{
return&mathS } public&void&setMathScore(String&mathScore)&{
this.mathScore&=&mathS }
@Override public&String&toString()&{
return&&学号:&+this.Sno+&&姓名:&+name+&&数学成绩:&+mathS } }
其他3条回答
为您推荐:
其他类似问题
您可能关注的内容
程序编写的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。(IT互联网猎头)
(我是阿喵酱)
(过路学员)
第三方登录:Java简明教程-习题参考答案 - 大学课件-
当前位置:
Java简明教程-习题参考答案
馆藏:21402
下载此文档
同系列文档
Baidu Button END -->
官方公众微信Java IO流题库
一、&&& 填空题
Java IO流可以分为& &节点流&& 和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行。
输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数据, read
方法给程序提供了一个从输入流中读取数据的基本方法。
read方法从输入流中顺序读取源中的单个字节数据,该方法返回字节值(0-255之间的一个整数),如果到达源的末尾,该方法返回& -1& &。
Java系统的标准输入对象是System.in,标准输出对象有两个,分别是标准输出System.out和标准错误输出____System.err__。
Java IO体系中,___ ObjectInputStream __是字节输入流,不仅提供了存取所有Java基础类型数据(如:int,double 等)和String的方法,也提供了提供存取对象的方法。
Java IO体系中,____ DataOutputStream __是字节输出流,提供了可以存取所有Java基础类型数据(如:int,double 等)和String的方法,但没有提供存取对象的方法。
___序列化__是指将Java对象转换成字节序列,从而可以保存到磁盘上,也可以在网络上传输,使得不同的计算机可以共享对象。
二、&&& 选择题
使用Java IO流实现对文本文件的读写过程中,需要处理下列(& B& )异常。(选择一项)
ClassNotFoundException
IOException
SQLException
RemoteException
在Java的IO操作中,(& D& )方法可以用来刷新流的缓冲。(选择两项)
void release()
void close()
void remove()
void flush()
在Java中,下列关于读写文件的描述错误的是(& B& )。(选择一项)
Reader类的read()方法用来从源中读取一个字符的数据
Reader类的read(int n )方法用来从源中读取一个字符的数据
Writer类的write(int n)方法用来向输出流写入单个字符
Writer类的write(String str)方法用来向输出流写入一个字符串
阅读下列文件定入的Java代码,共有( &C& )处错误。(选择一项)
import java.io.*;
public class TestIO {
public static void main(String []args){
String str ="文件写入练习";
FileWriter fw = null;&&&&&&& //1
fw = new FileWriter("c:\mytext.txt");& //2
fw.writerToEnd(str);&& //3
}catch(IOException e){&& //4
e.printStackTrace();
//此处省略关闭流
分析如下Java代码,有标注的四行代码中,有错误的是第( D )处。(选择一项)
import java.io.FileW
import java.io.IOE
public class Test {
public static void main(String[ ] args) {
String str = "Hello World";
FileWriter fw = null;
fw = new FileWriter("c:\\hello.txt"); // 1
fw.write(str); &&&&&&&&&&&&&&&&&&&&// 2
} catch (IOException e) {
e.printStackTrace(); &&&&&&&&&&&&&&// 3
} finally {
fw.close(); &&&&&&&&&&&&&&&&&&&&&&&// 4
以下选项中关于如下代码的说法正确的是( &AD& )。(选择二项)
public class TestBuffered {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new FileReader("d:/bjsxt1.txt"));
BufferedWriter bw =
new BufferedWriter(new FileWriter("d:/bjsxt2.txt"));
String str = br.readLine();
while(str !=null){
bw.write(str);
bw.newLine();
str = br.readLine();
br.close();
bw.close();
该类使用字符流实现了文件复制,将d:/bjsxt1.txt复制为d:/bjsxt2.txt
FileReader和FileWriter是处理流,直接从文件读写数据
BufferedReader和BufferedWriter是节点流,提供缓冲区功能,提高读写效率
readLine()可以读取一行数据,返回值是字符串类型,简化了操作
InputStreamReader是转换流,可以将字节流转换成字符流,是字符流与字节流之间的桥梁。它的实现使用的设计模式是( &C& )。(选择一项)
适配器模式
三、&&& 判断题
假设文件”a.txt”的长度为100字节,那么当正常运行语句”OutputStream f=new FileOutputStream(new File(“a.txt”));”之后,文件”a.txt”的长度变为0字节。(& T& )
ByteArrayInutStream和ByteArrayOutputStream对内存中的字节数组进行读写操作,属于字节流,属于处理流而不是节点流。 (& F& )
实现Serializable接口的可以被序列化和反序列化。该接口中没有定义抽象方法,也没有定义常量。(& T& )
序列化是指将字节序列转换成Java对象,只有实现了Serializable接口的类的对象才可以被序列化。(& F& )
四、&&& 简答题
输入流和输出流的联系和区别,字符流和字节流的联系和区别
列举常用的字节输入流和字节输出流并说明其特点,至少5对。
说明缓冲流的优点和原理
序列化的定义、实现和注意事项
五、&&& 编码题
1.实现字符串和字节数组之间的相互转换。必如将字符串“北京尚学堂bjsxt”转换为字节数组,并将字节数组再转换回字符串。
public class TestConvert
public static void main(String[] args) throws IOException
//准备一个字符串
String contents = " 近日,北京尚学堂科技有限公司正式成为央视网广告合作伙伴";
System.out.println(contents);
//String---byte []
byte[] buf = contents.getBytes();
//byte[]----String
String contents2 = new String(buf, 0, buf.length);
System.out.println(contents2);
2.实现字节数组和任何基本类型和引用类型执行的相互转换
提示:使用ByteArrayInutStream和ByteArrayOutputStream。
public class TestByteArrayStream
public static void main(String[] args) throws IOException,
ClassNotFoundException
int num = 50;
boolean flag =
User user = new User("bjsxt", "bjsxt");
//使用数据包把数据封装起来
//各种数据类型-----&byte[]
ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);//包装流
oos.writeInt(num);
oos.writeBoolean(flag);
oos.writeObject(user);
byte[] buf = baos.toByteArray();
baos.close();
//byte[]-----------&各种数据类型
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
ObjectInputStream ois = new ObjectInputStream(bais);
int num2 = ois.readInt();
boolean flag2 = ois.readBoolean();
User user2 = (User) ois.readObject();
System.out.println(num2);
System.out.println(flag2);
System.out.println(user2);
bais.close();
3.分别使用文件流和缓冲流复制一个长度大于100MB的视频文件,并观察效率的差异。
public class TestCopy4
public static void main(String[] args) throws IOException
//创建输入流和输出流
InputStream fis = new FileInputStream(new File("d:/1.mp4"));
OutputStream fos = new FileOutputStream("d:/2.mp4");
//使用输入流和输出流复制文件
byte[] buf = new byte[10];
int len = fis.read(buf);
while (len != -1)
fos.write(buf, 0, len);
len = fis.read(buf);
//System.out.println(len);
//关闭输入流和输出流
fis.close();
fos.close();
public class TestCopy
public static void main(String[] args) throws IOException
//创建输入流和输出流
InputStream fis = new FileInputStream(new File("d:/1.mp4"));
OutputStream fos = new FileOutputStream("d:/2.mp4");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//使用输入流和输出流复制文件
byte[] buf = new byte[10];
int len = bis.read(buf);
while (len != -1)
bos.write(buf, 0, len);
len = bis.read(buf);
//关闭输入流和输出流
bis.close();
bos.close();
4.复制文件夹d:/sxtjava下面所有文件和子文件夹内容到d:/sxtjava2。
提示:涉及单个文件复制、目录的创建、递归的使用
public class CopyDir
* 复制单个文件
* @param sourceFile 源文件
* @param targetFile 目标文件
* @throws IOException
public static void copyFile(File sourceFile, File targetFile) throws IOException
BufferedInputStream inBuff =
BufferedOutputStream outBuff =
// 新建文件输入流
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
while ((len = inBuff.read(b)) != -1)
outBuff.write(b, 0, len);
// 刷新此缓冲的输出流
outBuff.flush();
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
* 复制目录
* @param sourceDir 源目录
* @param targetDir 目标目录
* @throws IOException
public static void copyDirectiory(String sourceDir, String targetDir)
throws IOException
// 检查源目录
File fSourceDir = new File(sourceDir);
if (!fSourceDir.exists() || !fSourceDir.isDirectory())
System.out.println("源目录不存在");
//检查目标目录,如不存在则创建
File fTargetDir = new File(targetDir);
if (!fTargetDir.exists())
fTargetDir.mkdirs();
// 遍历源目录下的文件或目录
File[] file = fSourceDir.listFiles();
for (int i = 0; i & file. i++)
if (file[i].isFile())
File sourceFile = file[i];
// 目标文件
File targetFile = new File(fTargetDir, file[i].getName());
copyFile(sourceFile, targetFile);
//递归复制子目录
if (file[i].isDirectory())
// 准备复制的源文件夹
String subSourceDir = sourceDir + File.separator + file[i].getName();
// 准备复制的目标文件夹
String subTargetDir = targetDir + File.separator + file[i].getName();
// 复制子目录
copyDirectiory(subSourceDir, subTargetDir);
public static void main(String[] args) throws IOException
copyDirectiory("d:/sxtjava", "d:/sxtjava2");
1.使用IO包中的类读取D盘上exam.txt文本文件的内容,每次读取一行内容,将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。
public class Test
public static void main(String[] args) throws IOException
String path = "D:\\exam.txt";
outputMethod(path);
public static void outputMethod(String path) throws IOException
List&String& list = new ArrayList&String&(); // 创建集合对象
// 创建缓冲区对象
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine(); // 读取数据每次读一行
while (line != null)
list.add(line);
line = br.readLine();
br.close();
for (String s : list)
System.out.println(s);
2.假设从入学开始所有书写的Java类代码都在d:/sxtjava文件夹下,包括多级子文件夹。使用IO流获取从入学开始,到目前为止已经写了多少行Java代码。
其实就是获取d:/sxtjava文件夹及其子文件夹下的所有.java文件,使用readLine()读取其中每一行,每读取一行,行数加1。所有的文件读取完毕,得到总共已经写的Java代码行数。需要结合递归实现。
public class TestCountDir
* 统计一个java文件的行数
private void countLine(File sourceFile) throws IOException
BufferedReader br =
// 新建文件输入流
br = new BufferedReader(new FileReader(sourceFile));
while (br.readLine() != null)
//System.out.println(count);
br.close();
* 统计一个目录下所有Java文件的行数
private void countDir(String sourceDir) throws IOException
// 检查源目录
File fSourceDir = new File(sourceDir);
if (!fSourceDir.exists() || !fSourceDir.isDirectory())
System.out.println("源目录不存在");
// 遍历目录下的文件或目录
File[] file = fSourceDir.listFiles();
for (int i = 0; i & file. i++)
if (file[i].isFile())
if (file[i].getName().toLowerCase().endsWith(".java"))
// System.out.println(file[i].getName());
countLine(file[i]);
//递归统计代码行数
if (file[i].isDirectory())
// 准备统计的文件夹
String subSourceDir = sourceDir + File.separator + file[i].getName();
// 统计子目录
countDir(subSourceDir);
public static void main(String[] args) throws IOException
TestCountDir tcd = new TestCountDir();
tcd.countDir("d:/sxtjava");
System.out.println(tcd.count);
3.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行。
public class Student implements Comparable&Student&
//省略getter和setter方法
//省略构造方法
public int compareTo(Student stu)
return this.age - stu.
public String toString()
return "Student [age=" + age + ", name=" + name
+ ", num=" + num + "]";
public class Test
public static void main(String[] args)
Set&Student& stuSet = saveStudentInfo();
outputInfo(stuSet);
private static Set&Student& saveStudentInfo()
Scanner input = new Scanner(System.in);
// 保存学生信息的TreeSet集合对象
Set&Student& stuSet = new TreeSet&Student&();
while (true)
// 输入提示
System.out.println("请输入学生信息:(学号#姓名#年龄)");
String inputData = input.nextLine();
// 判断是否退出 inputData.equals("exit")
if ("exit".equals(inputData))
// 将用户输入的学生信息分割为String[]
String[] info = inputData.split("#");
// 将输入信息封装到Student对象中
Student stu
= new Student(Integer.parseInt(info[0]), info[1],
Integer.parseInt(info[2]));
// 将学生对象加入集合
stuSet.add(stu);
return stuS
private static void outputInfo(Set&Student& stuSet)
File file = new File("e:/student.txt");
// 创建文件输出流对象
FileWriter fw =
fw = new FileWriter(file);
Iterator&Student& it = stuSet.iterator();
while (it.hasNext())
String info = it.next().toString();
// 将info字符串,写入记事本
fw.write(info);
// 完成换行功能
fw.write("\r\n");
} catch (Exception e)
e.printStackTrace();
fw.close();
} catch (IOException e)
e.printStackTrace();
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
Java IO流分类以及主要使用方式如下: IO流 |--字节流 |--字节输入流 InputStream: int read();//一次读取一个字节 int read(byte[] bys);// ...
java io流 运行错误时,保存异常到文件里面 下面这个实例,运行后,输入数字,为正确,如果输入字符串,则报错,保存错误信息 //运行错误时,保存异常到文件里面 //下面这个实例,运行后,输入数字, ...
java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...
java io流 数据流传输 把这段当公式用就可以了 //数据流传输 import java.io.*; public class Index{ public static void main(Str ...
字符是我们能读懂的一些文字和符号,但在计算机中存储的却是我们看不懂的byte 字节,那这就存在关于字符编码解码的问题.所以在学习Io流的字符流前我们先了解些关于编码问题. 一.字符集与字符编码 1.什 ...
一.简单说明 1.打开数据库 int sqlite3_open( const char *filename,
// 数据库的文件路径 sqlite3 **ppDb
// 数据 ...
一.jQuery中的Ajax 1.jQuery为我们提供了更强大的Ajax封装 $.ajax({}) 可配置方式发起Ajax请求 $.get() 以GET方式发起Ajax请求 $.post() 以PO ...
http://www.cnblogs.com/ranran/p/4065619.html http://www.cnblogs.com/jxlsomnus/p/4450911.html 以下是关于AS ...
在两个不同的控制器之间传值是iOS开发中常有的情况,应对这种情况呢,有多种的应对办法.kvc就是其中的一种,所以,我们就在此解释之.
key value observing
键值观察,给人一种高 ...
来自: Zentaur(alles klar) 录一篇旧文 作者:王垠 完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作 ...
/// &summary& /// 导出Excel /// &/summary& /// &param name=&dt&&DataTable& ...
&div style=&border:1position:top:0float:width:100%&&删除 ...
原文转自:http://segmentfault.com/blog/windwhinny/2324 翻译的不好,轻拍 Polymer是google的一款前端开发框架,其基于Sh ...
软件包 java.util.concurrent 的描述 在并发编程中很常用的实用工具类.此包包括了几个小的.已标准化的可扩展框架,以及一些提供有用功能的类,没有这些类,这些功能会很难实现或实现起来冗 ...

我要回帖

更多关于 java键盘输入代码 的文章

 

随机推荐