我正在尝试操作 Java .txt 文件中包含的波浪号分隔表中的行和列数据。我已成功扫描/读取数据,但不确定如何操作它来镜像关系代数的操作,然后将输出作为波浪号分隔表(如原始文件一样)写入新的 .txt 文件,以便可以通过其他方法(如 project())读取。例如,我想创建一个 restrict()
方法,将输出限制为仅丰田汽车,因此驱动程序的主体可能如下所示:
//restrict the cars table to toyotas producing a table named toyotas
Algebra.Restrict("cars","MAKE='Toyota'","toyotas");
//project just three columns from the toyotas table producing a table named answer
Algebra.Project("Toyotas","Make,Model,Price","answer");
//display the contents of the answer table
Algebra.Display("answer");
输出为:
MAKE MODEL PRICE
----------------
Toyota Camry 18000
Toyota Tacoma 19000
Toyota Highlander 35000
来自 cars.txt 的输入
MAKE~MODEL~TYPE~PRICE
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000
Ford~Mustang~Sport~21000
Chevrolet~Corvette~Sport~48000
Ford~F150~Truck~25000
Toyota~Highlander~SUV~35000
对 algebra.Restrict("Ford", "", "Truck"); 使用以下代码:
public void Restrict(String a, String b, String c )throws FileNotFoundException, IOException{
{
Scanner x = null;
try
{
x = new Scanner(new File("cars.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<String[]> arr = new ArrayList<String[]>();
while (x.hasNext())
{
String str[] = x.next().split("~");
arr.add(str);
}
for (String[] column : arr)
{
if (column[0].equals(a))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
for (String[] column : arr)
{
if (column[1].equals(b))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
for (String[] column : arr)
{
if (column[2].equals(c))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
System.out.println("NEW TABLE SAVE TO DISK");
}
}
public void Project(String a)throws FileNotFoundException, IOException{
{
Scanner x = null;
try
{
x = new Scanner(new File("cars.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<String[]> arr = new ArrayList<String[]>();
while (x.hasNext())
{
String str[] = x.next().split("~");
arr.add(str);
}
for (String[] car : arr)
{
if (car[0].equals(a))
{
System.out.println(car[0] + " " + car[1] + " " + car[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("ProjectTable.txt");
outputfile.print(car[0] + " " + car[1] + " " + car[2]);
outputfile.close();
}
}
System.out.println("NEW TABLE SAVED TO DISK / ProjectTable.txt");
}
}
我得到的输出:
Ford Mustang Sport
Ford F150 Truck
Toyota Tacoma Truck
Ford F150 Truck
NEW TABLE SAVE TO DISK
所需输出
MAKE~MODEL~TYPE
Ford~F150~Truck
NEW TABLE SAVE TO DISK
或者至少
Ford F150 Truck
NEW TABLE SAVE TO DISK
最佳答案
第1步)将数据读入变量并创建数据对象。最简单的是数组和列表,如果您创建汽车类
并创建键值对会更好,但为了简单起见,这里是一个潜在的解决方案。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class cars
{
public static void main(String[] args)
{
Scanner x = null;
try
{
x = new Scanner(new File("cars.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<String[]> arr = new ArrayList<String[]>();
while (x.hasNext())
{
String str[] = x.next().split("~");
arr.add(str);
}
for (String[] car : arr)
{
if (car[0].equals("Toyota"))
{
System.out.println(car[0] + " " + car[1] + " " + car[2] + " " + car[3]);
}
}
}
}
输出:
Toyota Camry Sedan 18000
Toyota Tacoma Truck 19000
Toyota Highlander SUV 35000
我在 5 分钟内完成了此操作,但您可以轻松地将其用作模板来创建方法restrict
。模板类似于。
private boolean restrictBy (String make){
if make.equals("Totota") {
return true;
} else {
return false;
}
}
编辑:
File outFile = new File ("output.txt");
FileWriter fWriter = null;
try { fWriter = new FileWriter (outFile); }
catch (IOException e) { e.printStackTrace(); }
PrintWriter pWriter = new PrintWriter (fWriter);
for (String[] car : arr)
{
if (restrictBy(car, "Ford" , "", "Truck"))
{
System.out.println(car[0] + " " + car[1] + " " + car[2] + " " + car[3]);
pWriter.println(car[0] + " " + car[1] + " " + car[2] + " " + car[3]);
}
}
pWriter.close();
和方法restrictBy
private static boolean restrictBy (String[] car, String make, String model, String type) {
boolean filtered = true;
if (make.length() > 0 && !car[0].equals(make))
{
filtered = false;
}
if (model.length() > 0 && !car[1].equals(model))
{
filtered = false;
}
if (type.length() > 0 && !car[2].equals(type))
{
filtered = false;
}
return filtered;
}
关于java - 使用 Scanner 或 Buffer 读取 .txt 文件后在 Java 中操作表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453829/