Java 输入输出(含部分答案)
- 格式:doc
- 大小:107.00 KB
- 文档页数:19
输入输出流一、实验目的:熟悉Java的文件读写机制,练习输入输出流的使用。
二、实验内容:1、键盘输入10个整数,从小到大进行排序。
2、接收键盘输入的字符串,用FileInputStream类将字符串写入文件,用FileOutputStream类读出文件内容显示在屏幕上。
3、将一个文本文件的内容按行读出,每读出一行就顺序加上行号,并写入到另一个文件中。
三、实验要求:1. 通过实验掌握文件输入输出流的使用方法;2. 程序必须能够从键盘接收字符串并保存在文件中;3. 程序必须能够读出文件内容显示在屏幕上;4. 写出实验报告。
四、实验步骤:1、键盘输入10个整数,从小到大进行排序。
package javaTest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Gui_21 {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubBufferedReader br=new BufferedReader(newInputStreamReader(System.in));String str;int array[]=new int[10];for(int i=0;i<10;i++){System.out.println("请输入一个整数:");str=br.readLine();array[i]=Integer.parseInt(str);}for(int j=0;j<9;j++){for(int k=j+1;k<10;k++){if(array[k]<array[j]){int temp=array[k];array[k]=array[j];array[j]=temp;}}}for(int m=0;m<10;m++){if(m!=9)System.out.print(array[m]+",");elseSystem.out.print(array[m]);}}}2、接收键盘输入的字符串,用FileInputStream类将字符串写入文件,用FileOutputStream类读出文件内容显示在屏幕上。
Java笔试编程题输入输出处理1. 概述编程题的输入输出处理是Java编程中非常重要的一部分。
在笔试中,通常会给出一个具体的问题,要求我们编写代码实现相应的功能,并处理输入和输出。
本文将介绍Java编程题中输入输出处理的基本知识和常见的处理方法,帮助读者更好地应对Java笔试编程题。
2. 输入处理在编程题中,输入通常以标准输入的形式给出。
标准输入是指从键盘输入数据。
我们需要编写代码来读取这些输入数据,并进行相应的处理。
Java提供了Scanner类来方便地读取标准输入。
以下是一个示例代码:import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// 读取整数int num = scanner.nextInt();// 读取字符串String str = scanner.next();// 读取一行字符串String line = scanner.nextLine();// 读取浮点数double decimal = scanner.nextDouble();// 读取布尔值boolean flag = scanner.nextBoolean();// 其他操作...scanner.close();}}在以上示例代码中,我们使用Scanner类的不同方法来读取不同类型的数据。
nextInt()用于读取整数,next()用于读取字符串,nextLine()用于读取一行字符串,nextDouble()用于读取浮点数,nextBoolean()用于读取布尔值。
需要注意的是,使用nextLine()方法读取一行字符串时,可能会出现读取不完整的情况。
这是因为nextLine()会读取一行中的所有字符,包括换行符。
如果在读取前面的数据时使用了其他next()方法,那么换行符可能会被留在缓冲区中,导致nextLine()读取到的是一个空字符串。
JA V A数据的输入和输出一、数据的输出1、使用System.out.println(输出项)方法System.out.println(输出项);输出结果后换行。
输出项可以是变量名、常量、表达式。
例:public class ex1 {public static main(String args[]) {System.out.println("欢迎学习JA V A"); //输出: 欢迎学习JA V A System.out.println(10*2+4); //输出: 24System.out.println("a="+20); //输出: a=20}}2、使用System.out.print()方法System.out.print(输出项);在当前位置输出结果。
如:public class ex2 {public static void main(String args[]) {System.out.print("欢迎学习JA V A"); //输出: 欢迎学习JA V A System.out.print(10*2+4); //输出: 24System.out.println("a="+20); //输出: a=20System.out.println("b="+30);}}3、使用javax.swing.JOptionPane包的showMessageDialog()方法import javax.swing.JOptionPane;public class ex3 {public static void main(String[] args) {JOptionPane.showMessageDialog(null,"我学习JA V A,我快乐");System.exit(0);}}程序解释(1)语句import javax.swing.JOptionPane;是导入语句,为编译器找到程序中要使用的类。
1.在Java中,下列哪个类可以用于读取文本文件的字符?o A. FileInputStreamo B. FileReadero C. BufferedReadero D. InputStreamReader答案: B解析: FileReader是Java中用于读取文本文件字符的类,它建立于文件的基础上。
2.C语言中,使用fscanf函数从文件读取数据,其返回值表示什么?o A. 成功读取的字符数o B. 文件中的总字符数o C. 成功读取的数据项的数量o D. 文件的大小答案: C解析: fscanf函数返回成功读取和转换的数据项的数量。
3.下列哪个类用于在Java中创建输出文件流?o A. FileWritero B. FileOutputStreamo C. BufferedWritero D. OutputStreamWriter答案: B解析: FileOutputStream用于在Java中创建输出文件流。
4.假设你在C语言中使用fopen函数打开一个文件,但文件不存在于指定路径,函数将返回什么?o A. 正常文件指针o B. NULLo C. 错误代码o D. 空字符串答案: B解析: 如果文件不存在或无法打开,fopen将返回NULL。
5.在Java中,FileInputStream和FileOutputStream类是用于处理哪种类型的文件的?o A. 二进制文件o B. 字符文件o C. 文本文件o D. 图像文件答案: A解析: FileInputStream和FileOutputStream是用于读写二进制文件的。
6.C语言中的fwrite函数用于将数据写入文件。
假设你有一个结构体,你如何保证其完全写入文件?o A. 使用fwrite函数并指定结构体的大小o B. 使用fprintf函数o C. 使用fputc函数o D. 使用fputs函数答案: A解析: fwrite函数可以用于写入任意类型的数据,通过指定数据的大小确保结构体完全写入。
Java中常⽤数据类型的输⼊输出详解⽬录1、Char型1.1 输⼊格式:1.2 举例说明2、int型1.1 简单的int格式输⼊:1.2 举例说明2.1带空格的int格式输⼊:2.2 举例说明3.1 复杂int格式的输⼊3.2 举例说明3、double型1.1 double保留两位格式输出1.2 举例说明4、多次输⼊1.1 输⼊格式1.2 举例说明5、数组1.1 数组输⼊格式:2.1 数组转换成字符串6、字符串1.1 字符串转换成整型,浮点型(以整型为例)1.2 整型,浮点型转换成字符串2.1 字符串转换成字符数组2.2 字符数组转换成字符串3 举例说明7、写在最后C语⾔学习完转到Java后,第⼀个感觉就是Java的写法很复杂,同时,在常⽤的数据类型的输⼊输出上也没有C语⾔那么⽅便。
在C语⾔上⾯,⽤ scanf 函数可以很⽅便的输⼊⼤多数格式,在Java上却不⾏,Java当中没有类似于scanf的语句。
本⽂搭配本⼈的输⼊输出习惯及做题的记录,对这些不同类型的如整型的,是整型的但隔开参数的...做⼀个总结。
下⾯的说明中主类均为Main类,我们使⽤Scanner来进⾏输⼊。
每⼀种输⼊或输出可能有多种⽅法,本⼈只写了较为简单的写法。
1、Char型这⾥说的char型指的是只输⼊⼀个字符的情况。
1.1 输⼊格式:import java.io.IOException;//导⼊包public class Main {public static void main(String[] args) throws IOException {char ch = (char)System.in.read();//<1>System.out.println((int)ch);}}说明:需要搭配IOException异常来使⽤。
<1>中,System.in就是从标准输⼊流(最常见的是键盘)输⼊,rand()⽅法是从这个流⾥⾯读取输⼊的内容。
(Java)OJ在线编程常见输⼊输出练习场题解汇总⽬录1 A+B(1)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in);while(sc.hasNext()){int a = sc.nextInt();int b = sc.nextInt();System.out.println(a+b);}}}2 A+B(2)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); int n = sc.nextInt();while(n>0){int a = sc.nextInt();int b = sc.nextInt();System.out.println(a+b);n--;}}}3 A+B(3)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); while(sc.hasNext()){int a = sc.nextInt();int b = sc.nextInt();if(a==0 & b==0) break;System.out.println(a+b);}}}4 A+B(4)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); int len = sc.nextInt();while(len!=0){int sum = 0;for (int i=0;i<len;i++){sum = sum + sc.nextInt();}len = sc.nextInt();System.out.println(sum);}}}5 A+B(5)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); while(n>0){int sum = 0;String a = sc.nextLine();String[] as = a.split(" ");for(int i=1;i<as.length;i++){sum = sum + Integer.parseInt(as[i]); }System.out.println(sum);n--;}}}6 A+B(6)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){int sum = 0;String a = sc.nextLine();String[] as = a.split(" ");for(int i=1;i<as.length;i++){sum = sum + Integer.parseInt(as[i]); }System.out.println(sum);}}}7 A+B(7)import java.util.Scanner;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){int sum = 0;String a = sc.nextLine();String[] as = a.split(" ");for(int i=0;i<as.length;i++){sum = sum + Integer.parseInt(as[i]); }System.out.println(sum);}}}8 字符串排序(1)import java.util.Scanner;import java.util.Arrays;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); while(sc.hasNextLine()){String a = sc.nextLine();String[] as = a.split(" ");Arrays.sort(as);for(int i=0;i<n;i++){System.out.print(as[i]+' ');}// System.out.println(as[n-1]);}}}9 字符串排序(2)import java.util.Scanner;import java.util.Arrays;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){String a = sc.nextLine();String[] as = a.split(" ");Arrays.sort(as);for(int i=0;i<as.length;i++){System.out.print(as[i]+' ');}System.out.println("");}}}10 字符串排序(3)import java.util.Scanner;import java.util.Arrays;public class Main {public static void main(String[] args){Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){String a = sc.nextLine();String[] as = a.split(",");Arrays.sort(as);for(int i=0;i<as.length-1;i++){System.out.print(as[i]+',');}System.out.println(as[as.length-1]); }}}。
Java输入输出知识点:1、文件和流的操作,理解字节流和字符流的概念2、异常处理的概念,自定义异常类一、选择题1、如果一个程序段中有多个catch,则程序会按如下哪种情况执行( )A)找到合适的例外类型后继续执行后面的catchB)找到每个符合条件的catch都执行一次C)找到合适的例外类型后就不再执行后面的catchD)对每个catch都执行一次2、程序员将可能发生异常的代码放在()块中,后面紧跟着一个或多个()块。
A) catch、try B) try、catchC) try、exception D) exception、try3、在Java语言中,在程序运行时会自动检查数组的下标是否越界,如果越界,会抛掷下面的异常( )A) SQLException B) IOExceptionC) ArrayIndexOutOfBoundsExcetion D) SecurityManager4、在Java中,关于捕获异常的语法try-catch-finally的下列描述哪些正确( )A) try-catch必须配对使用B) try可以单独使用C) try-finally必须配对使用D) 在try-catch后如果定义了finally,则finally肯定会执行5、给定下面的代码片断:public class Animal{public void Cat(){try{Method();}catch(ArrayIndexOutBoundsException e){"Exception1");}catch(Exception e){"Exception2");}finally{"Hello World!!");}}public void Method(){}public static void main(String[] args){=new Animal ();Ani. Cat ();}}如果函数method正常运行并返回,会显示下面的哪些信息( )A) Hello World B) Exception1C) Exception2 D) Hello World!!6、如果下列的方法能够正常运行,在控制台上将显示什么( )public void example(){try{unsafe();"Test1");}catch(SafeException e){"Test 2");}finally{"Test 3");}"Test 4");}A) Test 1 B) Test 2C) Test 3 D) Test 47、以下哪一项不是File类的功能: ( )A) 创建文件B) 创建目录C) 删除文件D) 拷贝文件2、下面哪个不是InputStream类中的方法: ( )A) int read(byte[]) B) void flush()C) void close() D) int available()8、构造BufferedInputStream的合适参数是哪个A) BufferedInputStream B) BufferedOutputStreamC) FileInputStream D) FileOuterStreamE) File9、要从文件" "文件中读出第10个字节到变量C中,下列哪个方法适合( )A) FileInputStream in=new FileInputStream("");(9); int c=();B) FileInputStream in=new FileInputStream(""); (10); int c=();C) FileInputStream in=new FileInputStream(""); int c=();D) RandomAccessFile in=new RandomAccessFile(""); (9);int c=();10、以下程序发生什么异常class A {int x;public static void main {A x;}}A.IOExceptionB. InterruptExceptionC. NullPointerExceptionD. DataFormatException11、设有如下方法:public void test() {try {oneMethod();"condition 1");} catch (ArrayIndexOutOfBoundsException e) {"condition 2");} catch(Exception e) {"condition 3");} finally {"finally");}}如果oneMethod正常运行,则输出结果中有哪些A. condition 1B. condition 2C. condition 3D. finally12、设有如下代码:public void fun () {int i;try{i= ();"Location 1");} catch (IOException e) {"Location 2");} finally {"Location 3");}"Location 4");}如果有一个IOException发生, 则输出有哪些A. Location 1B. Location 2C. Location 3D. Location 413、设有如下代码:1 String s = null;2 if ( s != null & () > 0)3 "s != null & () > 0");4 if ( s != null && () > 0)5 "s != null & () > 0");6 if ( s != null || () > 0)7 "s != null & () > 0");8 if ( s != null | () > 0)9 "s != null | () > 0");以下行中哪些会产生空指针异常。
A. 2,4B. 6,8C. 2,4,6,8D. 2,6,814、类Test1、Test2定义如下:1.public class Test1 {2.public float aMethod(float a,float b)throws IOException { 3.}4.}5.public class Test2 extends Test1{6.7.}将以下哪种方法插入行6是不合法的。
A、float aMethod(float a,float b){ }B、public int aMethod(int a,int b)throws Exception{ }C、public float aMethod(float p,float q){ }D、public int aMethod(int a,int b)throws IOException{ }15、设有如下代码:try {tryThis();return;} catch (IOException x1) {"exception 1");return;} catch (Exception x2) {"exception 2");return;} finally {"finally");}如果tryThis() 抛出NumberFormatException,则输出结果是A. 无输出B. "exception 1", 后跟"finally"C. "exception 2", 后跟"finally"D. "exception 1"E. "exception 2"16、以下哪个是RandomAccessFile文件的构造方法: A.RandomAccessFile("data", "r");B.RandomAccessFile("r", "data");C.RandomAccessFile("data", "read"); D.RandomAccessFile("read", "data");17、设有如下代码:import .*;public class Th{public static void main(String argv[]){Th t = new Th();();}public void amethod(){try{ioCall();}catch(IOException ioe){}}}以下哪个最有可能是ioCall方法的方法体A.public void ioCall () throws IOException{DataInputStream din = new DataInputStream;();}B.public void ioCall () throw IOException{DataInputStream din = new DataInputStream;();}C.public void ioCall (){DataInputStream din = new DataInputStream;();}D.public void ioCall throws IOException(){DataInputStream din = new DataInputStream;();}18、当前目录不存在名为的文件,执行下面代码的输出结果为import .*;public class Mine{public static void main(String argv[]){Mine m=new Mine( );}public int amethod(){try{FileInputStream file=new FileInputStream("");}catch(FileNotFoundException e){"No such file found");return -1;}catch(IOException e){"Doing finally");}return 0;}}A. No such file foundB. No such file found-1C. No such file foundDoing finally-1D. 019、使用哪个类可创建目录A.File B.DataOutput C.Directory D.FileDescriptor E. FileOutputStream20、假设raf是一个随机访问文件,以下语句的编译和运行结果为( () ); A.代码不能编译.B.会出现IOExceptionC.文件指针将定位到文件的最后一个字符之前D.文件指针将定位到文件的最后一个字符21、以下哪些是FileOutputStream 构造方法的合法形式A.FileOutputStream( FileDescriptor fd )B.FileOutputStream( String n, boolean a )C.FileOutputStream( boolean a )D.FileOutputStream()E.FileOutputStream( File f )22、以下哪个能编译通过A.File f = new File("/","");B. DataInputStream d = new DataInputStream;C. OutputStreamWriter o = new OutputStreamWriter;D. RandomAccessFile r = new RandomAccessFile("OutFile");23、以下程序的调试结果为:import .*;class Base{public void amethod()throws FileNotFoundException{}}public class ExcepDemo extends Base{public static void main(String argv[]){ExcepDemo e = new ExcepDemo();}public void amethod(){}protected ExcepDemo(){try{DataInputStream din = new DataInputStream;"Pausing");();"Continuing");();}catch(IOException ioe) { }}}A. 由于构造方法为protected导致编译出错B. 由于amethod方法未声明异常导致编译出错C. 由于amethod方法未声明异常导致运行错误D. 输出显示"Pausing",击键后显示"Continuing"24、下列描述中不属于Java异常处理机制优点的一项是( D )。