Exercises

1.掌握打印流以及Scanner类的常用方法,使用打印流和Scanner优化之前的FileInputStream与FileOutputStream操作。要求:在桌面上新建一个Test.txt,使用打印流向文件中输出如下:

Hello 123

Hello bit

然后使用Scanner类读取文件内容并输入到控制台。

import java.io.*;
import java.util.Scanner;

/**
 * 要求:在桌面上新建一个Test.txt,使用打印流向文件中输出如下:
 * Hello 123
 * Hello bit
 * 然后使用Scanner类读取文件内容并输入到控制台。
 */
public class DemoIO {
    public static void main(String[] args) throws FileNotFoundException {
        File file=new File("C:\\Users\\Lenovo\\Desktop\\Test.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //定义PrintStream类,向文件中写数据
        PrintStream printStream=new PrintStream(new FileOutputStream(file));
        printStream.print("Hello ");
        printStream.println("123");
        printStream.print("Hello bit");
        printStream.close();//关闭流
        //使用Scanner类输入到控制台中
        Scanner scanner=new Scanner(new FileInputStream(file));
        scanner.useDelimiter("\r\n");//定义分隔模式
        while(scanner.hasNext()){
            System.out.println(scanner.next());
        }
    }
}

2.要求自定义Person类,其中三个属性name,age,school.age属性不作为序列化保存而其它两个属性使用序列化保存在本地文件Tester.txt文件中。使用序列化与反序列化的方式将自定义类序列化与反序列化操作。

import java.io.*;

/**
 * 要求自定义Person类,其中三个属性name,age,school.age属性不作为序列化保存而其它两个属性使用序列化保存在本地文件Tester.txt文件中。使用序列化与反序列化的方式将自定义类序列化与反序列化操作。
 */
class Person implements Serializable {
    private String name;
    private transient int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school='" + school + '\'' +
                '}';
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    private String school;
}
public class DemoIO {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        C:\\Users\\Lenovo\\Desktop\\Test.txtC:\\Users\\Lenovo\\Desktop\\Test.txt
//        "C"+File.separator+"Users"+File.separator+"Lenovo"+File.separator+"Desktop"+File.separator+"Test.txt"
        File file=new File("C:\\Users\\Lenovo\\Desktop\\Test.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        Person person=new Person();
        person.setName("张三");
        person.setAge(19);
        person.setSchool("陕科大");
       //对象序列化输出
//        ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file));
//        out.writeObject(person);
//        out.close();
        //读取
        ObjectInputStream in=new ObjectInputStream(new FileInputStream(file));
        Person per= (Person) in.readObject();
        System.out.println(per);
        in.close();
    }
}

3.输入一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

import java.io.*;
import java.util.Scanner;

/**
 *输入一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
 */

public class DemoIO {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
//    方式一:
//            int k=sc.nextInt();
//            int[] arr=new int[5];
//            for(int i=0;i<5;i++){
//                arr[i]=k%10;
//                k/=10;
//            }
//
//            if(arr[0]==arr[4]&&arr[1]==arr[3]){
//                System.out.println("是回文数");
//            }else{
//                System.out.println("不是回文数");
//            }
//     不限制长度 适用于任何回文串
            String k=sc.next();
            char[] ch=k.toCharArray();
            int begin=0;
            int end=ch.length-1;
            while(begin<end){
                if(ch[begin++]!=ch[end--]){
                    System.out.println("不是回文数");
                    break;
                }
            }
            if(begin>=end) {
                System.out.println("是回文数");
            }
        }

    }
}
发布了148 篇原创文章 · 获赞 32 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/smell201611010513/article/details/94167511