java - 无法将从文件读取的数据推送到通用堆栈
问题描述
我正在尝试创建一个名为 reverser 的通用类,它获取文本文件的内容并反转内容的顺序。我需要使用提供的通用堆栈类来执行此操作。我必须确保它适用于两种类型,字符串和浮点数,因为它们已在 main.xml 中实例化。
我知道我不能将 int 值(由 BufferedInputStream 返回)推送到堆栈上。我的问题是什么可以解决这个问题,我应该实例化两个不同版本的通用堆栈类,一个用于字符串,一个用于浮点数,还是有另一种解决方案?
public class project_5
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>();
Reverser<Float> majiGame = new Reverser<Float>();
}
}
class Reverser<E>
{
private Stack<E> tempStack;
int k = 0;
int val = 0;
public Reverser()
{
tempStack = new Stack<E>();
}
void FileToStack(String fileIn) throws Exception
{
E item = null;
try
{
BufferedInputStream readFile = new BufferedInputStream(new FileInputStream(fileIn));
for(k = 0; (val = readFile.read()) != -1; k++)
{
tempStack.push(val);
}
readFile.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(fileIn);
fileOut.println();
fileOut.close();
}
catch(FileNotFoundException e)
{
}
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}
编辑:删除图片,添加文本格式的代码。
解决方案
我已经更改了您正在读取文件然后放入的逻辑stack
,您还没有放入您的Node
课程,所以我相应地添加了
typecast
从文件读取时您需要自己的价值
注意:如果文件包含不正确的数据,您将收到转换错误,您需要处理
源代码.txt
1 2 3 4 5
目标.txt
5
4
3
2
1
代码
import java.io.*;
import java.util.Arrays;
public class Test
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>(String.class);
Reverser<Float> majiGame = new Reverser<Float>(Float.class);
majiGame.FileToStack("source.txt");
majiGame.StackToFile("target.txt");
}
}
class Reverser<E>
{
private Stack<E> tempStack;
private Class<E> tClass;
public Reverser(Class<E> tClass)
{
tempStack = new Stack<E>();
this.tClass = tClass;
}
public static <E> E convertInstanceOfObject(String value, Class<E> clazz) {
try {
// add custom casting, casting could be varies as par usage
if (clazz.getName().equals("java.lang.Integer")) {
return (E)Integer.valueOf(value);
} else if (clazz.getName().equals("java.lang.Float")) {
return (E)Float.valueOf(value);
} else if (clazz.getName().equals("java.lang.String")) {
return (E)value;
}
return clazz.cast(value);
} catch(ClassCastException e) {
return null;
}
}
void FileToStack(String fileIn) throws Exception
{
BufferedReader br;
try
{
br = new BufferedReader(new FileReader(fileIn));
String line = null;
while((line = br.readLine()) != null)
{
String[] valueArray= line.split(" ");
for (String v : valueArray) {
E item = convertInstanceOfObject(v, tClass);
tempStack.push(item);
}
}
br.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(new FileWriter(fileIn));
E d = null;
while (!tempStack.isEmpty()) {
d = tempStack.pop();
fileOut.println(d);
}
fileOut.close();
}
catch(IOException e)
{
}
}
}
class Node<E> {
Node<E> next;
private E data;
public Node(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}
推荐阅读
- python - 在 Python 中绘制 DWT 比例图
- list - Flutter 如何在 ListView 中加载 Future 列表?
- go - 如何在 Go 中向 AWS S3 预签名 URL 添加标头?
- javascript - 请问如何在等待几分钟后自动将余额添加到用户帐户?
- c++ - 为什么(或何时)模板引入其命名空间?
- google-apps-script - Google Apps 脚本创建 Google Doc,然后将 blob 保存到 PDF 即使在 saveAndClose() 之后也无法正常工作
- javascript - Jest 配置没有正确识别要测试的文件
- flutter - 向我的项目添加风味后,我无法初始化 Flutter for web
- python - 为什么 Ansible 不使用系统 python 的 pip?
- svg - SVG stroke-dashoffset 在 IOS(safari 和 chrome)上工作异常