자바 입력 의 몇 가지 상황

2880 단어 IO
package JavaTest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class InputDemo {
	
	public static void main(String[] args) throws IOException{
		//Demo_1();
		
		//Demo_2();
		
		//Demo_3();
		
		Scanner scr=new Scanner(System.in);
		BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\xuan.txt"));
		System.out.println("   ");
		String name=scr.next();
		System.out.println("result:"+name);
		bw.write(name);
		bw.newLine();
		bw.flush();
		bw.close();
		
	}

	public static void Demo_3() throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		String line=null;
		while((line=br.readLine())!=null){
			if(line.equals("#")){
				break;
			}
			//System.out.println("result:"+line);
			bw.write("result:"+line+"\r
"); //bw.newLine(); bw.flush(); } } ///////////////////////////// public static void Demo_2() throws IOException { // StringBuilder sb=new StringBuilder(); // InputStream in=System.in; // , int ch=0; while((ch=in.read())!=-1){ if(ch=='\r') continue; if(ch=='
'){ String temp=sb.toString(); if(temp.equals("#")){ break; } System.out.println(" :"+temp); sb.delete(0, sb.length()); }else{ sb.append((char)ch);// ( ) //System.out.println("result:"+ch); } } } ///////////////////////////////////// public static void Demo_1() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int len=0; while((len=br.read())!=-1){ System.out.println(" :"+(char)len); } br.close(); } }

첫 번 째: (정수 입력)
int a=System.in.read();
두 번 째: (만능 입력)
Scanner sc = new Scanner(System.in);
int  a = sc.nextInt();
java.util.Scanner sc = new java.util.Scanner(System.in);
int b = sc.nextInt();
public class InputSemo {
	
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		System.out.println("   1");
		int num=scanner.nextInt();
		System.out.println("   2");
		String string=scanner.nextLine();
		System.out.println("   3");
		String str =scanner.next();
		if(num==0){
			System.out.println("   ");
		}
		if(string==null){
			System.out.println("   ");
		}
		if(str==null){
			System.out.println("   ");
		}
		
		System.out.println(num+"=="+string+"=="+str);
	}

}

좋은 웹페이지 즐겨찾기