美文网首页
Java学习小记

Java学习小记

作者: 冯九岁 | 来源:发表于2018-11-28 02:25 被阅读0次

Scanner

Scanner s = new Scanner(System.in);
//  使用hasNextXxx()方法进行验证,再使用nextXxx()来读取

//  next()
//  next() 不能得到带有空格的字符串
import java.util.Scanner; 
 
public class ScannerDemo {  
    public static void main(String[] args) {  
        Scanner scan = new Scanner(System.in); 
        // 从键盘接收数据  
        //next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if(scan.hasNext()){   
          String str1 = scan.next();
          System.out.println("输入的数据为:"+str1);  
        }  
 
    }  
}

//  nextLine()
//  以Enter为结束符,可以获得空白
import java.util.Scanner; 
 
public class ScannerDemo {  
    public static void main(String[] args) {  
        Scanner scan = new Scanner(System.in); 
        // 从键盘接收数据  
        //nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if(scan.hasNextLine()){   
          String str2 = scan.nextLine();
          System.out.println("输入的数据为:"+str2);  
        }  
 
    }  
}

scanner.close();

数组大小用size()

students.size();

数组访问用get()

temp = students.get(i);

数组修改用set()

students.set(i, stu);

for的遍历

//  打印所有学生的信息
public void displayAllStudent() {
    if (students.size() > 0) {
        for (Student stu : students) {
            System.out.println("学号: " + stu.getSno() + "\t姓名: "
                    + stu.getSname() + "\t系部: " + stu.getSdept());
        }
    }else {
        System.out.println("数据库中无学生记录!");
    }
}

读取文件

// 读取文件获得原始数据
private void getData() {
    try {
        FileReader in_ = new FileReader("StudentDAO.txt");
        BufferedReader in = new BufferedReader(in_);
        String line;
        String reg1 = "\\s+";
        String str[] = new String[3];
        while ((line = in.readLine()) != null) {
            Student temp = new Student();
            str = line.split(reg1);
            temp.setSno(str[0]);
            temp.setSname(str[1]);
            temp.setSdept(str[2]);
            students.add(temp);
        }
        in.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

写入文件

//更新数据库,把数据输入文件
private void updateData() {
    try {
        FileWriter out_ = new FileWriter("StudentDAO.txt");
        BufferedWriter out = new BufferedWriter(out_);
        Student temp = new Student();
        for (int i = 0; i < students.size(); i++) {
            temp = students.get(i);
            out.write(temp.getSno() + " " + temp.getSname() + " "
                    + temp.getSdept() + "\r\n");
        }
        out.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

BufferedReader

do {
    //  从输入流读取一个字符并把该字符作为整数值返回, 当流结束的时候返回 -1
    c = (char)br.read();
    System.out.println(c);
} while (c != 'q');

do {
    str = br.readLine();
    System.out.println(str);
} while(!str.equals("end"));

FileInputStream(从文件读取数据)

InputStream f = new FileInputStream("C:/java/hello");

File f = new File("C:/java/hello");
InputStream out = new FileInputStream(f);

FileOutputStream(向文件中写数据)

OutputStream f = new FileOutputStream("C:/java/hello")

File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);

读取写入流最后都要close();

读写一定要用byte

byte bWrite[] = { 11, 21, 3, 40, 5 };
byte bWrite[] = "zhouzhiwenishandsome".getBytes();

相关文章

  • Java学习小记

    Scanner 数组大小用size() 数组访问用get() 数组修改用set() for的遍历 读取文件 写入文...

  • MVP+Rxjava+Retrofit

    前言 简单的示例DEMO,小记一下 RetrofitUtil.java BookAPI.java BookPres...

  • Java8学习小记

    2014年,Oracle发布了Java8新版本。对于Java来说,这显然是一个具有里程碑意义的版本。尤其是那函数式...

  • Java反射学习小记

    Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类 在运行时构造任意一个类的对象 在运行时判断...

  • Java 序列化

    参考链接:Java对象的序列化和反序列化Java transient关键字使用小记 1. Java序列化概念 把对...

  • 学生信息管理系统Plus

    StudentManagerPlus.java 小记:昨晚为了消除Eclipse中的警告,添加了sc.close(...

  • 学习笔记之transient关键字

    转载自:敏敏Alexia,Java transient关键字使用小记。 哎,虽然自己最熟的是Java,但很多Jav...

  • 学习笔记之transient关键字

    转载自:敏敏Alexia,Java transient关键字使用小记。 哎,虽然自己最熟的是Java,但很多Jav...

  • Java 小记

    判断是否是简单类型使用Spring中的BeanUtils,可以自行查看源码。 实际应用 判断某个对象是否是某个类或...

  • java小记

网友评论

      本文标题:Java学习小记

      本文链接:https://www.haomeiwen.com/subject/gspoqqtx.html