Flat file parsing with java - java

I want to parse below wmi output to hashmap as a key value pair using java.Please give me suggestions ..
My WMI Output contains 2 rows with multiple columns, first row is header and second row contains data. I want either regex or any approach to seperate the header with corresponding data as a key value for hashmap.
I am not getting any idea how to proceed...
Caption Description IdentifyingNumber Name
Computer System Product Computer System Product HP xw4600 Workstation
Parsing output should be like ...
Key = Value
Caption = Computer System Product
Description = Computer System Product
IdentifyingNumber =
Name = HP xw4600 Workstation

If your file format is always the same, you can easy use parser:
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
String[] array = new String[2];
for(int i = 0; i < 2; i++)
{
((BufferedReader)in).readLine();
}
for(int i = 0; i < array.length; i++)
{
array[i] = ((BufferedReader)in).readLine();
if(array[i] == null)
{
array[i] = ""; //$NON-NLS-1$
}
}
in.close();
String[] headers = array[0].split(Pattern.quote("\t")); //$NON-NLS-1$
String[] values = array[1].split(Pattern.quote("\t")); //$NON-NLS-1$
And then running through both and filling hashmap

I formatted the wmi output as a list and now its easy to formate the output.

Related

ArrayList<String> in PDF from a new row

I want to send some survey in PDF from java, I tryed different methods. I use with StringBuffer and without, but always see text in PDF in one row.
public void writePdf(OutputStream outputStream) throws Exception {
Paragraph paragraph = new Paragraph();
Document document = new Document();
PdfWriter.getInstance(document, outputStream);
document.open();
document.addTitle("Survey PDF");
ArrayList nameArrays = new ArrayList();
StringBuffer sb = new StringBuffer();
int i = -1;
for (String properties : textService.getAnswer()) {
nameArrays.add(properties);
i++;
}
for (int a= 0; a<=i; a++){
System.out.println("nameArrays.get(a) -"+nameArrays.get(a));
sb.append(nameArrays.get(a));
}
paragraph.add(sb.toString());
document.add(paragraph);
document.close();
}
textService.getAnswer() this - ArrayList<String>
Could you please advise how to separate the text in order each new sentence will be starting from new row?
Now I see like this:
You forgot the newline character \n and your code seems a bit overcomplicated.
Try this:
StringBuffer sb = new StringBuffer();
for (String property : textService.getAnswer()) {
sb.append(property);
sb.append('\n');
}
What about:
nameArrays.add(properties+"\n");
You might be able to fix that by simply appending "\n" to the strings that you collecting in your list; but I think: that very much depends on the PDF library you are using.
You see, "newlines" or "paragraphs" are to a certain degree about formatting. It seems like a conceptual problem to add that "formatting" information to the data that you are processing.
Meaning: you might want to check if your library allows you to provide strings - and then have the library do the formatting for you!
In other words: instead of giving strings with newlines; you should check if you can keep using strings without newlines, but if there is way to have the PDF library add line breaks were appropriate.
Side note on code quality: you are using raw types:
ArrayList nameArrays = new ArrayList();
should better be
ArrayList<String> names = new ArrayList<>();
[ I also changed the name - there is no point in putting the type of a collection into the variable name! ]
This method is for save values in array list into a pdf document. In the mfilePath variable "/" in here you can give folder name. As a example "/example/".
and also for mFileName variable you can use name. I give the date and time that document will created. don't give static name other vice your values are overriding in same pdf.
private void savePDF()
{
com.itextpdf.text.Document mDoc = new com.itextpdf.text.Document();
String mFileName = new SimpleDateFormat("YYYY-MM-DD-HH-MM-SS", Locale.getDefault()).format(System.currentTimeMillis());
String mFilePath = Environment.getExternalStorageDirectory() + "/" + mFileName + ".pdf";
try
{
PdfWriter.getInstance(mDoc, new FileOutputStream(mFilePath));
mDoc.open();
for(int d = 0; d < g; d++)
{
String mtext = answers.get(d);
mDoc.add(new Paragraph(mtext));
}
mDoc.close();
}
catch (Exception e)
{
}
}

How to include the unique id of each instance for sake of mapping in the future

I am using weka java API to classify couple of my instances, the file that I feed my weka file with is as follow:
0.3,0.1,1
0.0,0.04,0
0.0,0.03,1
And all of the above instances have unique id assigned to them for example the first row has id of 1098...
I wrote the following code which use weka java API to classify the result and return those instances that are classified incorrectly:
public static void SVM(ArrayList<String[]> testData) throws FileNotFoundException, IOException,
Exception {
BufferedReader breader = null;
breader = new BufferedReader(new FileReader("weka/train.txt"));
Instances train = new Instances(breader);
train.setClassIndex(train.numAttributes() - 1);
Instances unlabeled = new Instances(new BufferedReader(new FileReader(
"weka/test.txt")));
breader.close();
// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
// create copy
Instances labeled = new Instances(unlabeled);
LibSVM svm = new LibSVM();
svm.buildClassifier(train);
Evaluation eval = new Evaluation(train);
BufferedWriter writer = new BufferedWriter(new FileWriter(
"weka/labeledSVM.txt"));
for (int i = 0; i < unlabeled.numInstances(); i++) {
double clsLabel = svm.classifyInstance(unlabeled.instance(i));
if(unlabeled.instance(i).value(5)!=clsLabel){
writer.write("the unique id is: "+testData.get(i)[0] + " real label of the text is : "+ unlabeled.instance(i).toString() + ", According to Algorithm reult label is: " + clsLabel);
writer.newLine();
}
writer.flush();
writer.close();
}
But a big problem is that the mapping between the unique id and the instance labeled by algorithm is incorrect, so I am wondering if there is any way that I can include the unique id of each text inside the instances that I have but tell the weka classifier to ignore it ?
for example something like this:
1980,0.3,0.1,1
1981,0.0,0.04,0
1982,0.0,0.03,0
or any other suggestion is appreciated
The only way I found to do this was to create my own subclass of Instance.
Use "AddID" filter which will assign a uniqueID to every instance, then use FilteredClassifier i.e. weka.classifiers.meta.FilteredClassifier.

How do I write an array to .csv in Java?

I'm trying to write a piece of code in Java that generates an array of data, and I would like to write that data to a CSV file in one single column. However, I'm struggling with getting the correct output. My program generates the population in the array
double[] wasp = new double[1000];
which is populated by one of several functions, for instance:
for (int i = 0; i < wasp.length; i++) {
double mu = -10 + Math.random()*20;
double sigma = 0 + Math.random()*10;
wasp[i] = nextGaussian(mu, sigma);
description = "Normal";
Param1 = Double.toString(mu);
Param2 = Double.toString(sigma);
}
and I use the following code to try to write the array to CSV:
FileWriter writer = new FileWriter("C:\\Users\\Havok\\Google Drive\\Skripsie\\R\\JavaOut.csv");
for (int j = 0; j < wasp.length; j++) {
writer.append((char) wasp[j]);
writer.append(",");
}
writer.toString();
writer.flush();
writer.close();
However, when I open the CSV file, it looks "corrupt", as if the characters weren't encoded right or something. The data also fills up much more than one column in the file.
The output I expect is a CSV file that contains a single column of real values; for instance,
1.467354
0.812738
3.595733
and so on. However, what I'm getting is a column full of something like the following:
,,,,,,￶,,,,,,,￯,ï¿¿,,￾,,,,ï¿¿,,, ,,ï¿·,,￲,￲,,ï¿·,,,,,,ï¿·,,￸,￵,,,,,,￶,,,ï¿°,￸,,ï¿»,,￾,, ,,ï¿¿,,￾,,￯,,,,￵,,ï¿»,,↓,,ï¿·,￸,,,,ï¿
What am I doing wrong? I've looked at the tutorials on Java's home site and tried to adapt other similar solutions on StackOverflow, but it seems like I'm missing something crucial.
Simply convert your double to string using String.valueOf(double d) method.
FileWriter writer = new FileWriter("C:\\Users\\Havok\\Google Drive\\Skripsie\\R\\JavaOut.csv");
for (int j = 0; j < wasp.length; j++) {
writer.append(String.valueOf(wasp[j]));
writer.append("\n");
}
writer.close();
Alternatively you can use String.format() to format your double as you wanted.
In CSV format each set of data is delimited by a new line (\n) and each column is delimited by a comma. Therefore your code should look like this if you want a single column of data.
for (int j = 0; j < wasp.length; j++) {
writer.append((char) wasp[j]);
writer.append("\n");
}

Writing an Element object to file using java

I have a data of Element class. I'm trying to write its values to a file but I'm having trouble:
< Some process to acquire values into the variable "fieldData" >
// Prepare file output
FileWriter fstream = new FileWriter("C:/output.txt");
BufferedWriter out = new BufferedWriter(fstream);
Element field = fieldData.getElement(i);
out.write(field); // DOESN'T WORK: The method write(int) in the type BufferedWriter is not applicable for the arguments (Element)
out.write(field.getValueAsString()); // DOESN'T WORK: Cannot convert SEQUENCE to String
Any suggestions on how I should handle this case? In addition, what is the best way for me to see (i.e. print out to screen) the available static variables and methods associated with an object? Thx.
More code snippets to help debug:
private static final Name SECURITY_DATA = new Name("securityData");
private static final Name FIELD_DATA = new Name("fieldData");
Element securityDataArray = msg.getElement(SECURITY_DATA); // msg is a Bloomberg desktop API object
Element securityData = securityDataArray.getValueAsElement(0);
Element fieldData = securityData.getElement(FIELD_DATA);
Element field = fieldData.getElement(0)
out.write(field); // DOESN'T WORK: The method write(int) in the type BufferedWriter is not applicable for the arguments (Element)
out.write(field.getValueAsString()); // DOESN'T WORK: Cannot convert SEQUENCE to String
Turns out that this Bloomberg Prop data structure is long-winded to say the least:
private static final Name SECURITY_DATA = new Name("securityData");
private static final Name FIELD_DATA = new Name("fieldData");
Element securityDataArray = msg.getElement(SECURITY_DATA); // msg is a Bloomberg desktop API object
Element securityData = securityDataArray.getValueAsElement(0);
Element fieldData = securityData.getElement(FIELD_DATA);
Element field = fieldData.getElement(0);
/* the above codes were known at the time of the question */
/* below is what I was shown by a bloomberg representative */
Element bulkElement = field.getValueAsElement(0);
Element elem = bulkElement.getElement(0);
out.write(elem.name() + "\t" + elem.getValueAsString() + "\n");
whew...I don't think they try to make it easy! I'm also curious as to if there was a way that I could have figure this out by having Java print out the right method to use to trace down the data structure?
Element element = msg.GetElement("securityData");
for (int i = 0; i < element.NumValues; i++)
{
Element security = element.GetValueAsElement(i); //ie: DJI INDEX
Element fields = security.GetElement("fieldData");//ie: INDX_MEMBERS
for (int j = 0; j < fields.NumElements; j++)
{
Element field = fields.GetElement(j); //a list of members
for (int k = 0; k < field.NumValues; k++)
{
//print field.GetValueAsElement(k); //print members name
}
}
}
It sounds like you are trying to print the value of a input field element?
If so, then try:
out.write(field.getAttribute("value"));
Check out this one, for your second question:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html

Swt file dialog too much files selected?

the swt file dialog will give me an empty result array if I select too much files (approx. >2500files). The listing shows you how I use this dialog. If i select too many sound files, the syso will show 0. Debugging tells me, that the files array is empty in this case. Is there any way to get this work?
FileDialog fileDialog = new FileDialog(mainView.getShell(), SWT.MULTI);
fileDialog.setText("Choose sound files");
fileDialog.setFilterExtensions(new String[] { new String("*.wav") });
Vector<String> result = new Vector<String>();
fileDialog.open();
String[] files = fileDialog.getFileNames();
for (int i = 0, n = files.length; i < n; i++) {
if( !files[i].contains(".wav")) {
System.out.println(files[i]);
}
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(fileDialog.getFilterPath());
if (stringBuffer.charAt(stringBuffer.length() - 1) != File.separatorChar) {
stringBuffer.append(File.separatorChar);
}
stringBuffer.append(files[i]);
stringBuffer.append("");
String finalName = stringBuffer.toString();
if( !finalName.contains(".wav")) {
System.out.println(finalName);
}
result.add(finalName);
}
System.out.println(result.size())
;
I've looked at the FileDialog source code and I'm afraid, there is an upper boundary. A 32kB byte buffer for all 0-terminated filenames (if I understood it correctly).
So calculating with your values, if the medium size of your filname strings is around 12 characters, then you've hit exactly that upper boundary.
So the only way out is to select the files in two or more steps.

Categories

Resources