输入输出流,打印流,数据流

package ss;

import org.junit.Test;

import java.io.*;

/*
其他流的使用
1.标准的输入,输出流
2.打印流
3.数据流
/
public class OtherStreamTest {
/

1.标准的输入、输出流
1.1
System.in: 标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
1.2
System类的setIn(InputStream is)/setOut(PrintStream ps )方式重新指定输入和输出流。
1.3:从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,
直到当输入"e"或者"exit"时,退出程序
方法一:使用Scanner实现,调用next()方法返回一个字符串
方法二:System.in实现
/
/

public static void main(String[] args) {
BufferedReader br = null;
try {
System.out.println(“ss”);
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
String data = br.readLine();
if (“e”.equalsIgnoreCase(data) || “exit”.equalsIgnoreCase(data)) {
System.out.println(“程序结束”);
break;
}
String upperCase = data.toLowerCase();
System.out.println(upperCase);

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

/
/

2打印流:PrintStream 和 PrintWriter
2,1:提供一系列重载的print()和println()
2,2:练习:
*/
@Test
public void test1() {

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File("/Users/lidonglin/untitled4/src/ss/text11.text"));
        //创建打印输出流,设置为自动刷模式(写入换行字符或者字节"/n"时都会刷新输出缓冲区)
        PrintStream ps = new PrintStream(fos, true);
        if (ps != null) {//把标准的输出流(控制台输出)改为文件
            System.setOut(ps);
        }

        for (int i = 0; i <= 255; i++) {
            System.out.println((char) i);
            if (i % 50 == 0) {
                System.out.println();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

/*
 3,数据流
  3,1:DataInputStream 和DataOutStream
   3。2:作用:用于读取或者写出基本数据类型的变量或者字符串
 */
@Test
public void test3() throws IOException {
    FileOutputStream fos = new FileOutputStream("/Users/lidonglin/untitled4/src/ss/data.text");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeUTF("晚风");
    dos.flush();
    dos.writeInt(29);
    dos.flush();
    dos.writeBoolean(true);
    dos.flush();
    dos.close();
}
/*
读取数据:读取数据类型的数据要与写入文件的数据类型的数据一致
 */

@Test
public void test4() throws IOException {
    FileInputStream fos = new FileInputStream("/Users/lidonglin/untitled4/src/ss/data.text");
    DataInputStream dis = new DataInputStream(fos);
    String name = dis.readUTF();
    int age = dis.readInt();
    boolean Gender = dis.readBoolean();
    System.out.println(name);
    dis.close();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风strong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值