Inputstream e BufferedReader....help me!

G

gbattine

Hi Guys,
I've a problem and i need your help.
I have to develop a JSF application that allow user to upload a txt
file that has a known format. The application has to read the file, to
calculate the number of columns and rows with a Java routine and
showing them to user.

Later the application has to store the file into a byte's array in
accord to a known format(using the number of rows and columns) e has
to insert it in a blob field of a database.
Later the application has to do inverse procedure to show file to user
when it's required by a query.
Now i have developed a mini JSF application that allows file upload and
show to user information about file.
I've developed a java routine that receive a file and calculates the
number of row and columns.
How can i re-use my java routine in my JSF application?
I'm not able to do it.
Can anyone help me?I'm a newbie.....excuse me for my english...Thanks


The Java routine is



//package gbattine;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;
public class AddDb2 {
private static String fileName = "Dato2.txt";
private Map <String, double[]>dataMap = null;
private int Nrows=0;
private int Ncol=0;
private int numberOfNumericColumns =0;
public static void main(String[] args) throws IOException {
new AddDb2();
}

public AddDb2() throws IOException { //constructor
/* Get a reference to the input file. */
FileReader file = new FileReader(fileName);
BufferedReader br = new BufferedReader(file);
/* Create the empty map. */
this.dataMap = new HashMap<String, double[]>();/*Constructs an empty
HashMap
with the default initial capacity (16) and the default load factor
(0.75).*/
/* Populate the map from the input file. */
populateMap(br);
/*int Dim=(dataMap.size());//include the first string row
System.out.println("Il size del datamap e'"+Dim);*/
Ncol=(numberOfNumericColumns+1);
System.out.println("Il numero di colonne e'"+(Ncol));
//System.out.println("Il numero di colonne del file
è"+dataMap.size());
/* Display the contents of the map. */
displayMap(this.dataMap);
/* Close the reader. */
br.close();
}


/**
* Populate an HashMap from an input file.
*
* <p>This method accomodates an input file with any number of lines in
it. It also accomodates any number of doubles on the input line as long
as the
first value on the line is a String. The number of doubles on each
input line can also
vary.</p>
*
* @param bufferedReader
* @return a Map that has one element for each line of the input file;
the key is the first column from the input file and the entry is the
array
of doubles that follows the first column
* @throws IOException
*/
public Map populateMap(BufferedReader bufferedReader) throws
IOException
{
System.out.println("Caricamento dell'array di double in corso.....");
/* Store each line of the input file as a separate key and entry in
the Map. */
String line = null;
while ((line = bufferedReader.readLine()) != null) {
line = line.replace (',', '.');
Nrows++;
/* Create a tokenizer for the line. */
StringTokenizer st = new StringTokenizer(line);
/*
* Assuming that the first column of every row is a String and
the remaining columns are numbers, count the number of numeric columns.

*/
//int ColNumber=st.countTokens();
// int numberOfNumericColumns = ColNumber- 1;
numberOfNumericColumns = (st.countTokens()-1);
//System.out.println("Il numero di colonne del file
è"+dataMap.size());

/*
* Get the first token from the line. It will be a String and
its value will be a unique key for the rest of the row.
*/
String key = st.nextToken().trim();
/* Create the array for the numbers which make up the rest of the line.
*/
double[] array = new double[numberOfNumericColumns];
/* Populate the array by parsing the rest of the line. */
for (int column = 0; column < numberOfNumericColumns; column++){
array[column] = Double.parseDouble(st.nextToken().trim());
}
/* Store the first column as the key and the array as the entry.
*/
this.dataMap.put(key, array); /*Associates the specified value with
the specified key in this map.*/

}
System.out.println("Il numero di colonne numeriche del file
e'"+numberOfNumericColumns);
System.out.println("Il numero di righe del file e'"+Nrows);
//System.out.println("Il numero di colonne del file
è"+dataMap.size());
return this.dataMap;
}



public void displayMap(Map<String,double[]> myMap) {
/* Iterate through the values of the map, displaying each key and
its corresponding array. */
for (Map.Entry<String, double[]> entry : myMap.entrySet()) {
/* Get and display the key. */
System.out.print(entry.getKey() + " : ");
/* Get the array. */
double[] myArray = entry.getValue();
/*
* Display each value in the array. Put a semicolon after each
value except the last.
* Keep all the values for a given key on a single line.
*/
for (int ix = 0; ix < myArray.length; ix++) {
if (ix < myArray.length - 1) { //the value is not the last one in the
array ,allora metti ;
System.out.print(myArray[ix] + "; ");
} else { //the value is the last one in the array ,la linea finisce
così
System.out.println(myArray[ix]);
}
}
}
}
}


The web application Java Bean is MyBean.java

package com.devsphere.articles.jsfupload;

import org.apache.myfaces.custom.fileupload.UploadedFile;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import java.security.MessageDigest;
//import java.security.NoSuchAlgorithmException;

import java.io.*;

public class MyBean {
private UploadedFile myFile;
/*private String myParam;
private String myResult;*/

public UploadedFile getMyFile() {
return myFile;
}

public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>

<filter>
<filter-name>ExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.component.html.util.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>ExtensionsFilter</filter-name>
<servlet-name>FacesServlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD
JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.devsphere.articles.jsfupload.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/pages/MyForm.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/MyResult.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>


Grazie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top