system.io java_Java知识点总结(JavaIO- System类对IO的支持与Scanner类 )

Java知识点总结(JavaIO- System类对IO的支持与Scanner类 )

@(Java知识点总结)[Java, JavaIO]

[toc]

System类

295754f127165608ac22a1e1d30c097e.png

0c1f24bd73e9fbbb1461395cb99179d3.png

public class Demo09 {

public static void test1() {

try {

System.out.write("hello".getBytes());

} catch (IOException e) {

e.printStackTrace();

}

;

}

public static void test2() {

try {

System.out.write(Integer.parseInt("hello"));

} catch (Exception e) {

System.err.println(e);

}

;

}

// 输入的是英文没有问题,输入的是中文则会出现乱码,这是因为数据是一个字节一个字节读取进来的,汉字是2个字节

// 解决方法是将全部输入的数据放到缓冲区,然后一次性从内存中读取出来,这样数据只读一次,不会乱码

public static void test3() throws IOException {

System.out.println("请输入内容:" );

InputStream in = System.in;

int temp = 0;

StringBuffer sb = new StringBuffer();

while ((temp = in.read()) != -1) {

char c = (char) temp;

if (c == '\n') {

break;

}

sb.append(c);

}

in.close();

System.out.println("输入的内容是:" + sb.toString());

}

// 输出重定向。错误信息不在控制台显示,而是在err.log文件中

public static void test4() throws FileNotFoundException {

try {

System.out.write(Integer.parseInt("hello"));

} catch (Exception e) {

System. setOut(new PrintStream(new File("E:" + File.separator

+ "err.log")));

System.out.println(e);

}

;

}

// 输入重定向

public static void test5() throws Exception {

System. setIn(new FileInputStream(new File("E:" + File.separator

+ "err.log")));

InputStream in = System.in;

byte[] b = new byte[1024];

int len = in.read(b);

System.out.println(new String (b,0,len));

}

public static void main(String[] args) {

/*

* test1(); test2();

*/

/*

* try { test3(); } catch (IOException e) { e.printStackTrace(); }

*/

/*try {

test4();

} catch (FileNotFoundException e) {

e.printStackTrace();

}*/

try {

test5();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Sanner类

f7092cd29367d53bc82df8b29190b1ee.png

JDK 1.5后提供的输入数据类,此类位于java.util 包中,不仅可以完成输入数据的操作,还可以方便地对输入数据进行验证。

public class Demo11 {

public static void add() {

int i = getInt();

float j = getFloat();

System.out.printf("%d+%1.2f=%1.2f", i, j, i + j);

}

//从文件中读取数据

public static void getDataFromFile(){

File f = new File("E:"+File.separator+"test.txt");

StringBuffer sb = new StringBuffer();

try {

Scanner sc = new Scanner(f);

sc.useDelimiter("\n");// (设置读取的分隔符)空格处理

while(sc.hasNext()){ //读取下一行

sb.append(sc.next()+"\n");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

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

}

// 整数的输入

public static Integer getInt() {

Scanner sc = new Scanner(System.in);

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

if (sc.hasNextInt()) {

return sc.nextInt();

} else {

System.out.println("输入的不是整数,请重新输入:" );

return getInt();

}

}

// 小数的输入

public static Float getFloat() {

Scanner sc = new Scanner(System.in);

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

if (sc.hasNextFloat()) {

return sc.nextFloat();

} else {

System.out.println("输入的不是小数,请重新输入:" );

return getFloat();

}

}

//日期输入

public static Date getDate() {

Scanner sc = new Scanner(System.in);

String str = "";

System.out.println("请输入一个日期(yyyy-MM-dd):" );

if (sc.hasNext("^\\d{4}-\\d{2}-\\d{2}$")) {

str = sc.next();

try {

return new SimpleDateFormat("yyyy-MM-dd").parse(str);

} catch (ParseException e) {

e.printStackTrace();

return null;

}

} else {

System.out.println("您输入的不是日期,请重新输入!" );

return getDate();

}

}

public static void main(String[] args) {

/*

* Scanner sc = new Scanner(System.in); System.out.println("输入数据:");

* sc.useDelimiter("\n"); String str = sc.next();

* System.out.println("输入的数据为:" + str);

*/

//add();

//System.out.println(getDate());

getDataFromFile();

/*西瓜、 苹果、

香蕉

橘子

。。。*/

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写程序从文件读取数据,并输出到控制台。 ```java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFromFile { public static void main(String[] args) { try { File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } } } ``` 2. 编写程序从控制台读取数据,并将数据写入文件。 ```java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class WriteToFile { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter data: "); String data = scanner.nextLine(); scanner.close(); try { File file = new File("output.txt"); FileWriter writer = new FileWriter(file); writer.write(data); writer.close(); } catch (IOException e) { System.out.println("Error writing to file."); } } } ``` 3. 编写程序从一个文件复制数据到另一个文件。 ```java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFile { public static void main(String[] args) { try { File inputFile = new File("input.txt"); File outputFile = new File("output.txt"); FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } catch (IOException e) { System.out.println("Error copying file."); } } } ``` 4. 编写程序读取一个目录下的所有文件,并输出文件名和文件大小。 ```java import java.io.File; public class ListFiles { public static void main(String[] args) { File directory = new File("."); File[] files = directory.listFiles(); for (File file : files) { if (file.isFile()) { System.out.println(file.getName() + " (" + file.length() + " bytes)"); } } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值