import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class AddInData {
private String al=new String();
String path;
Scanner sc=new Scanner(System.in);
public void AddInData(String path){
//输入信息
al=sc.next();
//存入信息
File f=new File(path);
FileOutputStream fis=null;
try {
byte[] b=al.getBytes();
fis=new FileOutputStream(f);
fis.write(b);
fis.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
创建AddInData类里面有一个同名方法传入文件路径可以实现输入字符串把字符串存入文件里面
import java.io.IOException;
import java.util.Arrays;
public class ReadData {
public void ReadData(String path){
File f=new File(path);
FileInputStream fis=null;
try {
fis=new FileInputStream(f);
byte[] bytes=new byte[10];
fis.read(bytes);
for (int i=0;i<bytes.length;i++){
System.out.print((char) bytes[i]);
if (bytes[i+1]==0){break;}
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
我们创建ReadData方法用来读取文件的内容
public class GoodsSearchIn {
int index;
private String path;
public void GoodsSearchIn(int index){
switch (index){
case 1:
path="E:\\商品中心\\VivoY97.txt\\";
ReadData readData=new ReadData();
readData.ReadData(path);
break;
}
}
}
public class Main {
public static void main(String[] args) {
//以下为测试区
GoodsSearchIn goodsSearchIn=new GoodsSearchIn();
goodsSearchIn.GoodsSearchIn(1);
//以上为测试区
LoginInterface l=new LoginInterface();
l.LoginInterface();
}
}
添加一个GoodsSearchIn方法用户可以通过传入标记查询商品
import java.util.Scanner;
public class GoodsSearch {
Scanner sc=new Scanner(System.in);
GoodsSearchIn goodsSearchIn=new GoodsSearchIn();
public void GoodsSearch(){
System.out.println("这里是搜索框");
System.out.println("商品大全如下");
System.out.println("1-VivoY97");
System.out.println("用户请输入数字");
int sr=sc.nextInt();
goodsSearchIn.GoodsSearchIn(sr);
}
}
添加GoodsSearch方法指示用户