成都创新互联网站制作重庆分公司

java输入输出例之代码 java代码输出语句

Java的常用输入输出语句?

常用的输入语句是:

创新互联是一家专业从事网站设计制作、成都网站建设、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!

输入字符串:new Scanner(System.in).next();

输入整数:new Scanner(System.in).nextInt();

输入小数:new Scanner(System.in).nextDouble();

常用的输出语句:

换行输出: System.out.println(变量或字符串);

非换行输出: System.out.print(变量或字符串);

换行输出错误提示(默认是红字):System.err.println(变量或字符串);

不换行输出错误提示(默认是红字): System.err.print(变量或字符串));

用java编写的有输入输出流源代码

/**

* 读写指定文件或者读写指定某一个文件夹下的全部文件(不包括文件夹)

* @throws Exception

*/

public static void moveFile() throws Exception {

Scanner scan = new Scanner(System.in);

System.out.println("请输入源路径:");//输入源文件地址,可以是文件夹,可以是具体某个文件

String uDisk = scan.nextLine();

File file = new File(uDisk);//获得读取文件

if ((file.exists())) {//当文件存在

System.out.println("请输入目标路径:");//文件复制目标路径

String targetFolder = scan.nextLine();

File target = new File(targetFolder);//获得写入文件

if (!target.exists()) {

if (!target.mkdir()) {

throw new Exception("创建目标目录失败");

}

} else if (!target.isDirectory()) {

throw new Exception("与目标目录同名的文件已经存在");

}

File[] temp = null;

if(file.listFiles()==null || file.listFiles().length=0){

temp=new File[]{file};//输入的源路径指定文件,将文件添加到文件数组中

}else{

temp = file.listFiles();//如果输入的源路径是文件夹,则获取文件夹下文件的个数

}

if ((temp != null) (temp.length 0)) {//文件数组temp有值

int i = 0;

for (int length = temp.length; i length; i++) {//循环文件数组

if (!temp[i].isDirectory()) {

String fileName = temp[i].getName();

File t = new File(targetFolder + File.separator

+ fileName);//创建输出文件

if (!t.createNewFile()) {

throw new Exception("创建输出文件失败");

}

FileOutputStream out = new FileOutputStream(t);//创建文件输出流

FileInputStream in = new FileInputStream(temp[i]);//创建文件输入流

byte[] buffer = new byte[256];

while (in.read(buffer) 0) {//循环输入流,直到输入流无数据

out.write(buffer);//写入文件

}

}

}

}

}

}

调用例子:

public static void main(String[] args) throws Exception {

moveFile();

}

用JAVA写一个输入输出程序.

我知道的有两种方法可以实现.我写了2段程序你可以试一下.

第一种是把语句当成main参数输入.

public class Test1{

public static void main(String [] args){

System.out.print(args[0]);

}

}

在DOS提示符里调用时候用 java Test1 "Welcome to java" 用""否则会被当成字符串组被输入.

第二种是比较灵活的用System.in输入,直接调用程序即可.

import java.io.*;

import java.util.*;

public class Test

{

public static void main(String [] args){

try{

System.out.print("输入:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String st=(String)br.readLine();

System.out.println();

System.out.print("输出:"+st);

}catch(IOException e){}

}

}

java中怎样从键盘输入一个字符然后输出,求完整的程序

import java.io.*;

public class test

{

public static void main(String []args)

{

while (true)

{

try //-------用io库就一定要加上异常处理

{

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

System.out.println("请输入一个字符串:");

string s = br.readLine();//readLine()读入程序中的是一个字符串。

//java貌似从控制台读入的都是字符串,一般都要进行转化。

System.out.println(s);

System.out.println("请输入一个字符:");

char c = br.readLine().charAt(0);

System.out.println(c);

System.out.println("请输入一个双精度实数:");

double d = Double.parseDouble(br.readLine()); //转化为double型

System.out.println(d);

System.out.println("请输入一个单精度实数:");

float f = Float.parseFloat(br.readLine());

System.out.println(f);

System.out.println("请输入一个整数数:");

int i = Integer.parseInt(br.readLine());

System.out.println(i);

}

catch(IOException e){}

}

}

}

java输入一个字符串类似于hello,然后输出别的字符串代码要怎么写?

题目的意思

就是根据输入的不一样, 进行判断和输出

例一

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("请输入你的性别 男/女");

String sex = input.nextLine();//读取输入的一行

if(sex.equals("男")){//分情况输出

System.out.println("帅哥你好");

}else if(sex.equals("女")){

System.out.println("美女你好");

}else{

System.out.println("你好");

}

}

}

输出

请输入你的性别 男/女

帅哥你好

例二:

import java.util.Scanner;

public class PrintDemo {

public static void main(String[] args) {

String[] enNum = {"zero","one","two","three","four","five"};

System.out.println("请输入0~5的数字.系统转换成英文并输出");

Scanner input = new Scanner(System.in);//控制台输入

while(true){

String line = input.nextLine();

if(line.equals("exit")){

System.out.println("系统退出");

System.exit(0);

}else{

int x = Integer.parseInt(line);

System.out.println("数字"+x+"转成英语:"+enNum[x]);//控制台输出

}

}

}

}

运行测试

请输入0~5的数字.系统转换成英文并输出

2

数字2转成英语:two

3

数字3转成英语:three

exit

系统退出

java输入输出流与文件,求源代码!感谢大佬!

你好,java的API中提供了用于对象输入输出文件的操作,实例代码如下:

定义单词类如下(注意:你定义的类要实现Serializable接口)

public class Words implements Serializable {

private int size;

private String[] words;

public Words(){};

public Words(String...strs){

this.words = strs;

this.size = strs.length;

}

@Override

public String toString() {

return "Words{" +

"size=" + size +

", words=" + Arrays.toString(words) +

'}';

}

}

2. 对象输入输出api测试类

public class ObjIOTest {

public static void main(String[] args) {

String path = "d:/myIOTest.txt";

ObjIOTest objIOTest = new ObjIOTest();

Words words = new Words("hello", "my", "dear", "friend");

try {

objIOTest.writeObject(path,words);

Words wordsFromFile = (Words)objIOTest.readObject(path);

System.out.println(wordsFromFile.toString());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

//java serialize a object to file

public void writeObject(String path,Object map) throws IOException{

File f=new File(path);

FileOutputStream out=new FileOutputStream(f);

ObjectOutputStream objwrite=new ObjectOutputStream(out);

objwrite.writeObject(map);

objwrite.flush();

objwrite.close();

}

// read the object from the file

public Object readObject(String path) throws IOException, ClassNotFoundException{

FileInputStream in=new FileInputStream(path);

ObjectInputStream objread=new ObjectInputStream(in);

Object map=objread.readObject();

objread.close();

return map;

}

}

把两段代码拷贝到一个包下即可运行了,希望您的问题得到解答


分享标题:java输入输出例之代码 java代码输出语句
本文链接:http://cxhlcq.com/article/hishsi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部