BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
package 任务11IO流;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/** * @author ${范涛之} * @Description * @create 2021-11-25 1:16 */
public class Test1 {
public static void main(String[] args) throws IOException {
char c;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入字符,按下q退出程序");
do {
c = (char)bufferedReader.read();
System.out.println(c);
}while (c!='q');
}
}
package 任务11IO流;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/** * @author ${范涛之} * @Description * @create 2021-11-25 1:16 */
public class Test1 {
public static void main(String[] args) throws IOException {
/** * 读取字符 */
// char c;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// System.out.println("输入字符,按下q退出程序");
// do {
// c = (char)bufferedReader.read();
// System.out.println(c);
// }while (c!='q');
/** * 读取字符串 */
String str;
System.out.println("请输入字符串");
do {
str = bufferedReader.readLine();
System.out.println(str);
}while (!str.equals("end"));
}
}
InputStream f = new FileInputStream("C:/java/hello");
File f = new File("C:/java/hello");
InputStream in = new FileInputStream(f);
package 任务11IO流;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/** * @author ${范涛之} * @Description * @create 2021-11-25 1:54 */
public class Test2 {
public static void main(String[] args) throws FileNotFoundException {
File f = new File("F:/my.txt");
InputStream in = new FileInputStream(f);
System.out.println(in);
}
}
// 第二种方法:
package 任务11IO流;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/** * @author ${范涛之} * @Description * @create 2021-11-25 1:54 */
public class Test2 {
public static void main(String[] args) throws FileNotFoundException {
// File f = new File("F:/my.txt");
// InputStream in = new FileInputStream(f);
// System.out.println(in);
InputStream f = new FileInputStream("F:/my.txt");
System.out.println(f);
}
}
OutputStream f = new FileOutputStream("C:/java/hello")
File f = new File("C:/java/hello");
OutputStream fOut = new FileOutputStream(f);
package 任务11IO流;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
/** * @author ${范涛之} * @Description * @create 2021-11-25 2:02 */
public class Test3 {
public static void main(String[] args) throws FileNotFoundException {
OutputStream f = new FileOutputStream("F:/hello");
System.out.println(f);
}
}
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();
// 关闭输入流,释放系统资源
}
}
下面的例子创建 "/tmp/user/java/bin"文件夹:
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 + " 不是一个目录");
}
}
}
目录 /tmp
bin 是一个目录
lib 是一个目录
demo 是一个目录
test.txt 是一个文件
README 是一个文件
index.html 是一个文件
include 是一个目录
package 任务11IO流;
import java.io.File;
/** * @author ${范涛之} * @Description * @create 2021-11-25 2:13 */
public class Test4 {
public static void main(String[] args) {
String dirname = "F:/";
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 + " 不是一个目录");
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121528776
内容来源于网络,如有侵权,请联系作者删除!