java 输入,输出函数

输入函数

//整数
// 1. 导包,找到Scanner这个类在哪
//  书写要注意 要写在类定义的上面
import java.util.Scanner;

public class 输入函数 {
    public static void main(String[] args){
        //2.创建对象,表示我现在准备要用Scanner这个类
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入整数");

        //3.接收数据
        //变量i记录了键盘录入的数据
        int i = sc.nextInt();

        System.out.println(i);
    }
}

输出函数

public class 输出函数 {
    public static void main(String[] args) {
        //输出时从左到右依次执行
        System.out.println("123" + 123);
        System.out.println(1 +99 +"黑马程序员");
        System.out.println("abc" + true);
        System.out.println("中" + "abc" + true);
        int i = 18;
        System.out.println("我的年龄是"+ i + "岁");
        System.out.println(1 + 2 +"abc" + 2 + 1);


        System.out.printf("你好啊%s","彭于晏\n");
        System.out.printf("%s说你好啊%s","彭于晏","铁生");
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java输入输出和文件操作是Java编程中非常基本和重要的部分。Java提供了许多类来处理输入输出和文件操作,如File类、InputStream、OutputStream、Reader、Writer等。这些类可以用来读取和写入文件和其他类型的数据。 Java的文件操作通常包括文件的创建、读取、写入和删除。例如,我们可以使用File类的构造函数创建一个新文件,使用FileInputStream和FileOutputStream读取和写入二进制文件,或使用BufferedReader和PrintWriter读取和写入文本文件。 以下是一些常用的Java输入输出和文件操作的示例代码: 读取文本文件: ``` String fileName = "sample.txt"; try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.err.println(e.getMessage()); } ``` 写入文本文件: ``` String fileName = "output.txt"; String message = "Hello world!"; try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) { writer.println(message); } catch (IOException e) { System.err.println(e.getMessage()); } ``` 读取二进制文件: ``` String fileName = "sample.bin"; try (InputStream in = new FileInputStream(fileName)) { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { // process the buffer } } catch (IOException e) { System.err.println(e.getMessage()); } ``` 写入二进制文件: ``` String fileName = "output.bin"; byte[] data = {1, 2, 3, 4, 5}; try (OutputStream out = new FileOutputStream(fileName)) { out.write(data); } catch (IOException e) { System.err.println(e.getMessage()); } ``` 创建和删除文件: ``` String fileName = "newfile.txt"; File file = new File(fileName); try { if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.err.println(e.getMessage()); } if (file.delete()) { System.out.println("File deleted: " + file.getName()); } else { System.out.println("Failed to delete file."); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值