利用io流 书写简单的本地存储学生的学生管理code

学习了io流,就把它用到了以前写的简单学生管理系统上,因为本人还未研究数据库,无法对数据长久存储,就利用io,以及字符串的切割函数对数据进行文件存储,代码如下

首先我最近看了面向接口编程即(设计一个类遵循依赖倒置原则—>降低耦合性,接口隔离原则—>将复杂的接口细化,避免一些实现类有一些没用的方法等..)

我先建立了一个people类,有一个显示人信息的方法,以及得到属性的方法..即使我们里面有get与set方法.....

package Jave_Demo4_24;

public interface IPeople {
public void show();
public String getname();
public String getnid();
}

接下来是Student实现类

package Jave_Demo4_24;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class Student implements IPeople{
private String name;
private String nid;
Scanner input = new Scanner(System.in);
static boolean k = true;
String str = "f:"+File.separator+"io"+File.separator+"Student.txt";
private static ArrayList<Student> arraylist = new ArrayList<Student>();

private void printTitle(){
System.out.println("====欢迎来到学生管理系统!====");
System.out.println("1.增学生,2删除学生,3改学生,4查学生,5显示学生,6退出");
System.out.println("=======================");
}
public Student(){
printTitle();
System.out.println("系统已有学生为:");
updateStudent();
while(k){
System.out.println("请输入操作符(1-5)>");
Scanner inputa = new Scanner(System.in);
int n = StudentTest.checkNum(inputa);

switch (n) {
case 1:
addStu();
break;
case 2:
delStu();
break;
case 3:
changeStu();
break;
case 4:
findStu();
break;
case 5:
show();
break;
case 6:
k = false;
break;
default:
k = false;
break;
}
}
}
public Student(String name,String nid){
this.name = name;
this.nid = nid;
}

public void addStu(){
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生学号:");
String nid = input.next();
boolean f = checkStu(nid);
if(f == true){
String student = "学生:"+name+" "+"学号:"+nid+" ";
byte[] b = student.getBytes();
writeStu(b,true);
arraylist.add(new Student(name,nid));
System.out.println("添加成功....");
}else{
System.out.println("已有该学生,验证后再输入");
}
}

public boolean checkStu(String id){
for(Student item:arraylist){
if(item.nid.equals(id)){
return false;
}
}
return true;
}

public int findStu(){
System.out.println("请输入学生学号:");
String id = input.next();
int count = 0;
for(Student item:arraylist){
if(item.nid.equals(id)){
System.out.println("该的学生信息为:"+item);
return count;
}
count++;
}
return -1;
}

public void delStu(){
int pos = findStu();
if(pos != -1){
arraylist.remove(pos);
System.out.println("删除成功...");
}else{
System.out.println("学生不存在,请查证后操作...");
}
updateDelStudent();
}

    public void changeStu(){
int pos = findStu();
if(pos != -1){
Student stu = arraylist.get(pos);
System.out.println("请重新输入姓名:");
stu.name = input.next();
System.out.println("请重新输入学号:");
stu.nid = input.next();
System.out.println("设置成功");
}else{
System.out.println("学生不存在,请查证后操作...");
}
updateDelStudent();
}
    
    public void updateStudent(){
    byte[] byteStu = readStu();
    if(byteStu == null){
    System.out.println("初始化文件....无数据...");
    }else{
    String stu =new String(byteStu);
    System.out.println(stu);
    int pos = stu.indexOf(":");
    int posa = 0;
    while(pos != -1){
    posa = stu.indexOf(" ");
    String stuname = stu.substring(pos+1, posa);
    stu = stu.substring(posa+1);
    pos = stu.indexOf(":");
    posa = stu.indexOf(" ");
    String stunid = stu.substring(pos+1, posa);
    arraylist.add(new Student(stuname,stunid));
    stu = stu.substring(posa+1);
    pos = stu.indexOf(":");
    }
    }
    }
    
    public void updateDelStudent(){
    boolean f = false;
    for(Student item:arraylist){
    String student = "学生:"+item.name+" "+"学号:"+item.nid+" ";
    byte[] b = student.getBytes();
    writeStu(b,f);
    f = true;
    }
    }
    
public byte[] readStu(){
File file = new File(str);
FileInputStream fileinput = null;
byte[] b = new byte[2048];
try {
fileinput = new FileInputStream(file);
try {
int num = fileinput.read(b);
if(num == -1){
System.out.println("无数据");
return null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileinput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}

public void writeStu(byte[] b,boolean f){
File file = new File(str);
FileOutputStream fileoutput = null;
try {
if(f == true){
fileoutput = new FileOutputStream(file,f);
fileoutput.write(b);
}else{
fileoutput = new FileOutputStream(file);
fileoutput.write(b);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
fileoutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void show() {
// TODO Auto-generated method stub
System.out.println("姓名:"+"\t"+"学号:");
for(Student item:arraylist){
System.out.println(item);
}
}


@Override
public String toString() {
return "学生:" + name + "\t   " + nid ;
}


@Override
public String getname() {
// TODO Auto-generated method stub
return name;
}


@Override
public String getnid() {
// TODO Auto-generated method stub
return nid;
}

}

最后建立测试类,简单作业请勿见笑......

package Jave_Demo4_24;
import java.util.Scanner;


public class StudentTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
new Student();
}

public static int checkNum(Scanner input){      //简单处理一下输入操作符的异常.....
int i = 0;
try {
i = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入有误,操作符为整数(1-6)");
System.out.println("重新输入操作符,非(1-6)的整数,均视为6.退出");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
int size = str.length();
int num=0;
for(int j=0;j<size;j++){
num = num + (int)str.charAt(j);
}
return num;
}
return i;
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是如何使用 IO 将 hbase 查询结果存储到本地文件中的示例代码: ``` import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; // 省略其他代码 // 创建 HBase 连接、获取表对象等 Table table = connection.getTable(TableName.valueOf(tableName)); // 创建 Scan 对象 Scan scan = new Scan(); // 设置要查询的列族 scan.addFamily(Bytes.toBytes(columnFamily)); // 执行查询操作,获取结果 ResultScanner scanner = table.getScanner(scan); // 创建 FileOutputStream 对象,用于写文件 FileOutputStream fos = new FileOutputStream(new File(filePath)); // 遍历查询结果 for (Result result : scanner) { // 将结果转为字符串 String resultStr = Bytes.toString(result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column))); // 将字符串写入文件 fos.write(resultStr.getBytes()); fos.write("\n".getBytes()); } // 关闭 fos.close(); scanner.close(); table.close(); ``` 在上面的代码中,我们首先使用 HBase 的 API 执行查询操作,并获取结果。然后创建了一个 FileOutputStream 对象,用于写文件。接下来,我们使用一个循环遍历查询结果,并将每一行结果转为字符串,然后写入文件。最后,关闭和 HBase 相关的资源。 注 ### 回答2: 在HBase中查出的表可以通过以下步骤利用IO流存储到本地文件中: 1. 首先,通过HBase的Java API连接到HBase集群,并获取要读取的表对象。 2. 使用Table类的scan方法创建一个扫描器对象,以遍历表中的所有行。 3. 在扫描器对象上设置所需的过滤器(如指定列族、列限定符或行键范围等),以提取特定数据。 4. 创建一个BufferedWriter对象,用于将数据写入本地文件。 5. 使用扫描器对象的next方法逐行读取表中的数据。 6. 对于每一行数据,可以使用行键、列族和列限定符等信息来访问和处理特定的列数据。 7. 将每一行的数据转换为字符串格式,并使用BufferedWriter对象的write方法将数据写入本地文件中。 8. 循环执行步骤6和步骤7,直到遍历完整个表的数据。 9. 关闭扫描器对象和BufferedWriter对象,以释放资源并确保数据已经写入文件中。 10. 至此,HBase表中的数据已经通过IO流保存到本地文件中。 需要注意的是,在处理大量数据时,可以考虑使用分页读取数据的方式,以避免内存溢出的问题。此外,对于较大的表,可以考虑使用多线程或分布式的方式进行数据读取和写入,以提高效率。 ### 回答3: 在HBase中查询到的表可以通过IO流存储到本地文件中。首先,我们需要连接HBase集群并获取HBase的Java客户端对象。然后,创建一个用于存储数据的本地文件,并使用IO流将查询到的数据写入该文件。 下面是一个示例代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class HBaseToTextFile { public static void main(String[] args) { try { // 配置HBase Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181"); // 创建HBase的Java客户端对象 Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("表名")); // 创建本地文件,准备写入数据 String localFilePath = "/本地路径/文件名.txt"; BufferedWriter writer = new BufferedWriter(new FileWriter(localFilePath)); // 查询HBase表 Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); // 遍历查询结果,并将每一条数据写入本地文件 for (Result result : scanner) { for (Cell cell : result.listCells()) { String rowKey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); String columnFamily = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()); String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); // 将数据写入本地文件 writer.write(rowKey + "\t" + columnFamily + ":" + qualifier + "\t" + value); writer.newLine(); } } // 关闭资源 scanner.close(); table.close(); connection.close(); writer.close(); System.out.println("数据已成功存储到本地文件:" + localFilePath); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中,我们使用HBase的Java客户端接口来查询HBase表的数据,使用IO流将数据逐行写入本地文件。请替换代码中的`zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181`为你的ZooKeeper集群地址,将`表名`替换为你要查询的表名,将`/本地路径/文件名.txt`替换为你想要存储数据的本地文件路径和文件名。 运行代码后,查询到的表数据将会以每行一条的格式存储在本地文件中,每一行的格式为:行键 列族:列名 值。运行完成后,控制台会显示数据已成功存储到本地文件的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值