文件输入输出

import com.sun.jdi.Value;

import javax.sql.rowset.serial.SQLOutputImpl;
import java.awt.*;
import java.io.*;
import java.security.Key;
import java.util.*;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`,
// 然后按 Enter 键。现在,您可以在代try码中看到空格字符。
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
try {
int m = Integer.parseInt("12");
String n = m + "";
if (n.matches("\\d+")) {
throw new Exception("8888");
}
System.out.println(n);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
System.out.println("111");
}
String s = "W,E,R,G,H";
String[] s1 = s.split(",", s.length());
for (String s2 : s1) {
System.out.println(s2);
}
Student1 s4=new Student1("智",12);
System.out.println(s4.toString());
Date today=new Date();
String q=String.format(Locale.CHINESE,"%tb",today);
System.out.println(q);
Random r=new Random();
int i=r.nextInt(10,234);
System.out.println(i);
Date date=new Date();
System.out.println(String.format("%tY:%tm:%td:%tH:%tM:%tS",date,date,date,date,date,date));
int a=1000000000;
System.out.println(String.format("%+,d",a));
String n="123";
int j=Integer.parseInt(n);
System.out.println(j);
String n2="true";
Boolean j2=Boolean.parseBoolean(n2);
System.out.println(j2);
String e="qwe";
System.out.println(e.toUpperCase());
HashMap<Integer,Integer>map=new HashMap<>();
map.put(1,2);//被覆盖
map.put(3,4);
map.put(1,5);
map.put(6,7);
map.put(8,2);
map.forEach((Key, Value)-> System.out.println(Key+" "+Value));
Collection<Integer> m=map.values();
System.out.println(m);
Set<Integer>m2=map.keySet();
System.out.println(m2);
ArrayList<String>Arr=new ArrayList<>();
Arr.add("11");
Arr.add("22");
Arr.add("33");
String []s6=new String[Arr.size()];
String []s7=Arr.toArray(s6);
for (int i1 = 0; i1 < s7.length; i1++) {
System.out.println(s7[i1]);
}
//不允许二次读,因为读完一次后指针会到最后;
//可以关闭再重新打开
FileReader file=new FileReader("C:\\Users\\赵福奇\\Desktop\\mmm\\ttt.txt");
int io;
File f=new File("123.txt");
FileWriter file2=new FileWriter(f,true);
if(!f.exists())
{
f.createNewFile();
System.out.println("成功");
}
while((io=file.read())!=-1)
{
file2.write(io);
file2.append("iiu");
System.out.print((char)io);
}
file.close();
file2.close();
/* FileReader fileReader = new FileReader("C:\\Users\\赵福奇\\Desktop\\mmm\\ttt.txt");
FileWriter fileWriter = new FileWriter("123.txt");
int data;

while ((data = fileReader.read()) != -1) {
fileWriter.write(data);
}

fileReader.close();
fileWriter.close();

// 重新打开文件
fileReader = new FileReader("C:\\Users\\赵福奇\\Desktop\\mmm\\ttt.txt");

while ((data = fileReader.read()) != -1) {
// 这里可以再次处理文件内容
}

fileReader.close();
System.out.println("文件复制成功");*/

BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("1234.txt",true));//不加true默认覆盖,加了是追加
bufferedWriter.write("34sd反而还挺5wertyuykiUI6");
bufferedWriter.append("34sd反而还挺5wertyuykiUI6");
bufferedWriter.newLine();
bufferedWriter.close();

BufferedReader bufferedReader=new BufferedReader(new FileReader("C:\\Users\\赵福奇\\Desktop\\mmm\\ttt.txt"));
String S1;
while((S1=bufferedReader.readLine())!=null)
{
System.out.println(S1);
}
bufferedReader.close();
PrintStream printStream=new PrintStream(new FileOutputStream("3456.txt",true));
printStream.print("dfdg");
printStream.println(1223);
printStream.printf("%.2f",2324567.23456);
printStream.close();
PrintWriter printWriter=new PrintWriter(new FileWriter("5678.txt",true));
printWriter.write("qewretr");
printWriter.close();
/* RandomAccessFile randomAccessFile = new RandomAccessFile("78910.txt", "rw");
// 写入一个整数
int g1 = 1;
randomAccessFile.writeInt(g1);
// 写入一个字符串
String text = "sdfg";
randomAccessFile.writeBytes(text);
// 移动文件指针到开头
randomAccessFile.seek(0);
// 读取整数
int readInt = randomAccessFile.readInt();
System.out.println("读取整数: " + readInt);
// 读取字符串
byte[] byteArray = new byte[text.length()];
randomAccessFile.readFully(byteArray);
String readString = new String(byteArray);
System.out.println("读取字符串: " + readString);
randomAccessFile.close();*/
student sp=new student("1","2",3);
System.out.println(sp.toString());
apple apple1=new apple();
apple1.eat();
System.out.println(apple1.fuzhi());


}
public static class Student1
{
public String name;
public int age;

public Student1(){}
public Student1(String name, int age) {
this.name = name;
this.age = age;
}



public String toString()
{
return /*super.toString()+*/name+age;
}
}
public static class student extends Student1
{

public String b;
public student(String a,String b,int c)
{
super(b,c);
this.b=a;
}
}
abstract static class fruit
{
public String color;
public String fuzhi()
{
color="Red";
return color;
}
public abstract void eat();
}
static class apple extends fruit
{
public void eat()
{
System.out.println("chi");
}
}
}
posted @ 2023-09-25 15:16  赵千万  阅读(6)  评论(0编辑  收藏  举报