OK. I need to revise my question. My previous question is too vague to show my problem.

Above screenshot shows my Dynamic Web Project. It has several Java code, which works correctly as a stand-alone.
Also, I have 'index.jsp' file, which is inside:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PFBankingSystem</title>
</head>
<body>
<%
PFSystemTemplate PF = new PFSystemTemplate();
PF.init();
PF.filterConnection();
PF.filterExecution();
%>
PF...blahblah code is same with code originally inside main function. So, clicking 'run on server' means just running my original stand alone program, just using .jsp. (It is similar just using jsp as a console. I will add more interface later.)
Then, my original java program tries to read 'BankingDataExample.dat' using Java File I/O. (You can find this file in above screenshot. I just put it on both Project root folder and WebContent folder.)
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));
...
public static final String BANKING_DATA_FILE = "BankingDataExample.dat";
Then, my program keeps saying it cannot find 'BankingDataExample.dat'.

I don't know about how to use servlet, but if using servlet is only solution, I will try. But at this point, I just want to find proper location that I can put my input file.
=======================For providing more information, I put two full source=============
SourceFilter.java
public class SourceFilter extends PFGeneralFilter {
public SourceFilter() {
super();
}
@Override
public void compute() throws EndOfStreamException {
try {
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));
String bankingDatum = "";
while((bankingDatum = brForBankingData.readLine()) != null) {
this.outPorts[Constant.DEFAULT_PORT].write(Utility.convertStringToByteArray(bankingDatum));
}
brForBankingData.close();
throw new EndOfStreamException();
} catch (IOException e) {
throw new EndOfStreamException();
}
}
}
PFGeneralFilter.java
public abstract class PFGeneralFilter implements Runnable, PFFilterInterface {
protected PipedInputStream[] inPorts;
protected PipedOutputStream[] outPorts;
public PFGeneralFilter() {
this.inPorts = new PipedInputStream[]{new PipedInputStream()};
this.outPorts = new PipedOutputStream[]{new PipedOutputStream()};
}
public PFGeneralFilter(int numberOfInputPorts, int numberOfOutputPorts) {
this.inPorts = new PipedInputStream[numberOfInputPorts];
for(int i = 0; i < numberOfInputPorts; i++)
this.inPorts[i] = new PipedInputStream();
this.outPorts = new PipedOutputStream[numberOfOutputPorts];
for(int i = 0; i < numberOfOutputPorts; i++)
this.outPorts[i] = new PipedOutputStream();
}
private void closePorts() {
try {
for(int i = 0; this.inPorts != null && i < this.inPorts.length; i++)
this.inPorts[i].close();
for(int i = 0; this.outPorts != null && i < this.outPorts.length; i++)
this.outPorts[i].close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
try {
compute();
} catch (Exception e) {
if (e instanceof EndOfStreamException)
return;
}
}
}
@Override
public void connect(int indexForOutputPortInThisFilter, PipedInputStream connectedInputPortInNextFilter) throws IOException {
this.outPorts[indexForOutputPortInThisFilter].connect(connectedInputPortInNextFilter);
}
@Override
public PipedInputStream getInputPort(int index) {
return this.inPorts[index];
}
@Override
public PipedOutputStream getOutputPort(int index) {
return this.outPorts[index];
}
protected class EndOfStreamException extends Exception {
private static final long serialVersionUID = 1L;
public EndOfStreamException() {
closePorts();
}
}
abstract public void compute() throws EndOfStreamException;
}
I am not sure if you are doing that in servlet or JSP, say you put the file just in webContent directory, you can access the file like this in servlet or JSP
String path = request.getServletContext().getRealPath("/") + BANKING_DATA_FILE;
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));
I do not see the JAVA class PFSystemTemplate, but in you jsp you really need to pass a path which is where the application is deployed, you need to make the class and use it like this:
PFSystemTemplate PF = new PFSystemTemplate(request.getServletContext().getRealPath("/"));
the parmameter will need to be passed to the SourceFilter's constructor. and of course you need add a constructor to the class PFSystemTemplate.
Modified SourceFilter may look like this:
public class SourceFilter extends PFGeneralFilter {
private String appPath;
public SourceFilter(String appPath) {
super();
this.appPath = appPath;
}
@Override
public void compute() throws EndOfStreamException {
try {
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(appPath + Constant.BANKING_DATA_FILE)));
String bankingDatum = "";
while((bankingDatum = brForBankingData.readLine()) != null) {
this.outPorts[Constant.DEFAULT_PORT].write(Utility.convertStringToByteArray(bankingDatum));
}
brForBankingData.close();
throw new EndOfStreamException();
} catch (IOException e) {
throw new EndOfStreamException();
}
}
}
Try this:
ServletContext servletContext = request.getSession().getServletContext();
String path = servletContext.getRealPath("/BankingDataExample.dat");
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));
If above code is not okay, Try this:
BufferedReader br = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/BankingDataExample.dat"), "Windows-1252"));
In case, your file under /WEB-INF/classes/BankingDataExample.dat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With