吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
//使用 BufferedReader 在控制台读取字符 import java.io.*; public class BRRead {
public static void main(String args[]) throws IOException {
char c;
// 使用 System.in 创建 BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入字符, 按下 'q' 键退出。");
// 读取字符
do {
c = (char) br.read();
System.out.println(c);
} while (c != 'q');
}
}
//使用 BufferedReader 在控制台读取字符
import java.io.*; public class BRReadLines {
public static void main(String args[]) throws IOException {
// 使用 System.in 创建 BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'end' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while (!str.equals("end"));
}
}
import java.io.*; //演示 System.out.write().
public class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
import java.io.*; public class fileStreamTest {
public static void main(String args[]) {
try {
byte bWrite[] = { 11, 21, 3, 40, 5 };
OutputStream os = new FileOutputStream("test.txt");
for (int x = 0; x < bWrite.length; x++) {
os.write(bWrite[x]); // writes the bytes
}
os.close(); InputStream is = new FileInputStream("test.txt");
int size = is.available(); for (int i = 0; i < size; i++) {
System.out.print((char) is.read() + " ");
}
is.close();
} catch (IOException e) {
System.out.print("Exception");
}
}
}
//文件名 :fileStreamTest2.java
import java.io.*; public class fileStreamTest2 {
public static void main(String[] args) throws IOException { File f = new File("a.txt");
FileOutputStream fop = new FileOutputStream(f);
// 构建FileOutputStream对象,文件不存在会自动新建 OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk writer.append("中文输入");
// 写入到缓冲区 writer.append("\r\n");
// 换行 writer.append("English");
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入 writer.close();
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉 fop.close();
// 关闭输出流,释放系统资源 FileInputStream fip = new FileInputStream(f);
// 构建FileInputStream对象 InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
// 构建InputStreamReader对象,编码与写入相同 StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
// 转成char加到StringBuffer对象中
}
System.out.println(sb.toString());
reader.close();
// 关闭读取流 fip.close();
// 关闭输入流,释放系统资源 }
}
import java.io.File; public class CreateDir {
public static void main(String args[]) {
String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
// 现在创建目录
d.mkdirs();
}
}
import java.io.File; public class DirList {
public static void main(String args[]) {
String dirname = "/tmp";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println("目录 " + dirname);
String s[] = f1.list();
for (int i = 0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " 是一个目录");
} else {
System.out.println(s[i] + " 是一个文件");
}
}
} else {
System.out.println(dirname + " 不是一个目录");
}
}
}
import java.io.File; public class DeleteFileDemo {
public static void main(String args[]) {
// 这里修改为自己的测试目录
File folder = new File("/tmp/java/");
deleteFolder(folder);
} // 删除文件及目录
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
}
吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO的更多相关文章
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- 吴裕雄--天生自然 JAVA开发学习:解决java.sql.SQLException: The server time zone value报错
这个异常是时区的错误,因此只你需要设置为你当前系统时区即可,解决方案如下: import java.sql.Connection ; import java.sql.DriverManager ; i ...
- 吴裕雄--天生自然 JAVA开发学习:基础语法
package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...
- 吴裕雄--天生自然 JAVA开发学习:Scanner 类
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...
- 吴裕雄--天生自然 JAVA开发学习:方法
/** 返回两个整型变量数据的较大值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) re ...
- 吴裕雄--天生自然 JAVA开发学习:正则表达式
import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String conten ...
- 吴裕雄--天生自然 JAVA开发学习:日期时间
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...
- 吴裕雄--天生自然 JAVA开发学习:String 类
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...
随机推荐
- 杭电oj3306:Another kind of Fibonacci(矩阵快速幂)
Another kind of Fibonacci 题目链接 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 干货分享:学术Essay写作流程及写作技巧详解
Academic essay是指留学生作业中的一种,其范围非常广泛,可以是任何一种话题.而学术essay主要是指其中比较正式的.客观的话题,有明确的研究目的与研究对象.例如“Research on t ...
- 第十篇 Form表单
Form表单 阅读目录(Content) Form介绍 普通的登录 使用form组件 Form那些事儿 常用字段演示 校验 使用Django Form流程 补充进阶 应用Bootstrap样式 批量添 ...
- Python pip设置为清华镜像
设置为默认镜像 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
- c++程序—浮点数
#include<iostream> using namespace std; int main() { //2.单精度float //3.双精度double //默认情况下会输出6位有效 ...
- POJ 1584:A Round Peg in a Ground Hole
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5741 Acc ...
- export环境变量
/etc/profile和/etc/profile.d/区别 [root@zzx conf]# vim /etc/profile.d/tomcat.sh 添加如下内容再运行脚本就可以添加环境变量 ...
- C# 基本元素
一.构成C#的基本元素 注释和空白编译器不会编译,自动忽略:而标记是可以通过编译器编译的. 关键字 (keyword) 官方定义:关键字是类似标识符的保留的字符序列,不能用作标识符(以 @ 字符开头时 ...
- 吴裕雄--天生自然TensorFlow2教程:填充与复制
import tensorflow as tf a = tf.reshape(tf.range(9), [3, 3]) a tf.pad(a, [[0, 0], [0, 0]]) tf.pad(a, ...
- C#高级编程(第9版) 第08章 委托、lambda表达式和事件 笔记
本章代码分为以下几个主要的示例文件: 1. 简单委托 2. 冒泡排序 3. lambda表达式 4. 事件示例 5. 弱事件 引用方法 委托是寻址方法的.NET版本.在C++中函数 ...