i want read text file in java8, i am getting error "Type mismatch: cannot convert from FileReader to Reader". If I change Reader class to FileReader than I get error "The constructor BufferedReader(FileReader) is undefined"
My statements are
Reader fr = new FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
Please suggest
To confirm that you are having a class with the name FileReader, just use the full class name in the code :
java.io.Reader fr = new java.io.FileReader("testfile.txt");
java.io.BufferedReader br = new java.io.BufferedReader(fr);
This will assure that you use the specific class and not a yourPackage.FileReader class.
Then, since only FileReader seems to be problematic, you can clean it a bit like :
import java.io.*
...
Reader fr = new java.io.FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
Only specifying the FileReader full name.
NOTE:
using Class.GetPackage, you should find out which class you are using.
System.out.println(FileReader.class.getPackage());
Explanation:
JLS - 7.5. Import Declarations
The scope and shadowing of a type or member imported by these declarations is specified in §6.3 and §6.4.
6.4.1. Shadowing
A package declaration never shadows any other declaration.
A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:
any top level type named n declared in another compilation unit of p
any type named n imported by a type-import-on-demand declaration in c
any type named n imported by a static-import-on-demand declaration in c
Example
A
A.Run
A.Test
B
B.Test
In A.Run.java
System.out.println(Test.class.getPackage());
Here is the output :
Without import : Package A
Without import import B.* : Package A
Without import import B.Test : Package B
You're probably importing something other than java.io.BufferedReader and java.io.Reader.
This works
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Reader fr = new FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
}
}
Please check your imports;
Related
I am writing some code that takes each line of a txt file and stores it into a string. Afterward, the program will make a new file and store write the array into it.
This is the contents of the file:
04/26/16 Sega 3D Classics Collection
07/14/16 Batman: Arkham Underworld
06/24/16 Tokyo Mirage Sessions #FE
The problem with my code is that it doesn't seem to store or make a new file once the method is running. The method to make the file into a string array works but it doesn't seem to take that array and write it on a brand new file. What I have tried is to use the FileWriter function to make a new file on my computer and use the writer function to write the array onto the file. Whenever I run or debug the program there is no new file in my computer.
This is the code I have:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main{
public static void main (String[]args) throws FileNotFoundException{
File file = new File("releasedates.txt");
input(file);
}
public static String[]input (File file) throws FileNotFoundException{
String[]arr = new String[3];
Scanner sc = new Scanner(file);
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextLine();
}
return arr;
}
public static void output(String filename, String[] info) throws IOException{
FileWriter writer = new FileWriter("fileName.txt");
writer.write(filename);
writer.close();
}
}
If you're hoping that running your main() method will read and write back out the contents of the file I'm seeing the following things that are preventing you from having the result you're looking for:
Your main() never calls the output() method that actually writes to the file.
Your output() method will write the value of fileName to the file, not the value of the info array that you pass in. You may have to call Arrays.toString(info) or iterate through its contents so the FileWriter can process it correctly.
I have a model obtained from weka classifier and I want to test it in java code, But when I read instances, an error appear:
Exception in thread "main" java.io.IOException: keyword #relation expected, read Token[Word], line 1
at weka.core.Instances.errms(Instances.java:1863)
at weka.core.Instances.readHeader(Instances.java:1740)
at weka.core.Instances.<init>(Instances.java:119)
at licenta1.LoadModelWeka.main(LoadModelWeka.java:18)
My code is:
package licenta1;
import weka.core.Instances;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.trees.J48;
import weka.classifiers.Evaluation;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader ;
public class LoadModelWeka
{
public static void main(String[] args) throws Exception {
// training
BufferedReader reader = null;
reader=new BufferedReader(new FileReader("D:\\aaaaaaaaaaaaaaaaaaaaaa\\Licenta\\BioArtLicTrainSetTask1.csv"));
Instances train =new Instances (reader);
train.setClassIndex(0);
reader.close();
NaiveBayes nb = new NaiveBayes();
nb.buildClassifier(train);
Evaluation eval = new Evaluation(train);
eval.crossValidateModel(nb, train, 10 , new Random(1));
System.out.println(eval.toSummaryString("\n Results \n=====\n",true));
System.out.println(eval.fMeasure(1)+" "+eval.precision(1)+" "+eval.recall(1)+" ");
}
}
Can somebody help me?
Mt training set is in .csv format
This snippet is useful to directly load csv content and convert them to Instances. Usually .arff is used for weka operations and this loader directly converts csv files to arff internally and then to Instances class.
CSVLoader loader = new CSVLoader();
loader.setSource(new File("filename.csv"));
Instances trainingDataSet = loader.getDataSet();
Instead of using Buffered Reader you can try
DataSource source = new DataSource("/some/where/data.arff");
For more information visit this link http://weka.wikispaces.com/Use+WEKA+in+your+Java+code
im using weka jar 3.7.10, and this is how i can load csv using weka :
DataSource source1 = new DataSource("D:\\aaaaaaaaaaaaaaaaaaaaaa\\Licenta\\BioArtLicTrainSetTask1.csv");
Instances pred_test = source1.getDataSet();
I am trying to convert pptx files to txt (Text Extraction) using Apache POI Framework (Java).
I'm new in coding Java, so I don't know a lot about Buffered Readers/InputStream, etc.
What I tried is:
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
... Classes and Stuff ....
String inputfile = "X:\\Master\\simpl_temp\\2d0a44a2-95e7-428c-911c-1f803acbff42.pptx";
InputStream fis = new FileInputStream(inputfile);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fis));
String fileName = br1.readLine();
System.out.println(new XSLFPowerPointExtractor(new XMLSlideShow(new XSLFSlideShow(fileName))).getText());
br1.close();
My goal is, to write the extracted text into a variable, but It doesn't even work to print it on console... What I get is:
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'PK
org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:69)
org.apache.poi.xslf.XSLFSlideShow.<init>(XSLFSlideShow.java:90)
Any help would be greatly appreciated!
You are doing much to much, in fact you are trying to read the data of the PPTX itself as filename, better simply use
System.out.println(new XSLFPowerPointExtractor(
new XMLSlideShow(new XSLFSlideShow(
"X:\\Master\\simpl_temp\\2d0a44a2-95e7-428c-911c-1f803acbff42.pptx"))).getText());
or more generic
POITextExtractor extractor = ExtractorFactory.createExtractor(
new java.io.File("X:\\Master\\simpl_temp\\2d0a44a2-95e7-428c-911c-1f803acbff42.pptx"");
System.out.println(extractor.getText());
extractor.close();
I cannot give you the correct answer (because I myself don't use POI), but I can tell you where your mistake might lie.
The constructor of the class XSLFSlideShow is expecting file path as its argument. But you are passing an InputStream. Try it as follows:
String filePath = "X:\\Master\\simpl_temp\\2d0a44a2-95e7-428c-911c-1f803acbff42.pptx";
System.out.println(new XSLFPowerPointExtractor(new XMLSlideShow(new XSLFSlideShow(filePath))).getText());
I am trying to combine 4 files into a single file,using SequenceInputStream and LinkedList as data structure.
My error is
Exception in thread "main" java.lang.ClassCastException:
java.io.FileInputStream cannot be cast to java.util.Enumeration at
faizal.Address.main(Address.java:21)
import java.io.*;
import java.util.*;
public class Address{
public static void main(String[] args) throws Exception {
FileInputStream f1 = new FileInputStream("E://Ass.java");
FileInputStream f2 = new FileInputStream("E://Ass1.java");
FileInputStream f3 = new FileInputStream("E://abc.txt");
FileInputStream f4 = new FileInputStream("E://ad.txt");
LinkedList al = new LinkedList();
al.add(f1);
al.add(f2);
al.add(f3);
al.add(f4);
Collections.synchronizedList(al);
Enumeration e = (Enumeration) al.element();
SequenceInputStream sq = new SequenceInputStream((Enumeration<? extends InputStream>) al);
int i = 0;
while((i=sq.read())!=-1){
System.out.print((char)i);
}
}
}
If I understand your goal: you have 4 files, you want to concatenate. Format inside files doesn't matter. The way you use is useless (sic), and cant work, because your program cant guess magicly what is inside.
Then, just :
read each one:
Reading a plain text file in Java
I suppose it is texte (txt, java) : just append to String, and write in one shot to a file, like this:
How do I create a file and write to it in Java?
or, write each one, and append to the file
How to append text to an existing file in Java
import java.lang.*;
import java.util.*;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
public class Test
{
public static void main(String[] args) throws IOException
{
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
boolean[] correctAnswers = new boolean[20];
infile.close();
}
}
I'm getting these errors for some reason:
C:\Users\Rawr\Documents\Test.java:11: cannot resolve symbol
symbol : class Scanner
location: class Test
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
^
C:\Users\Rawr\Documents\Test.java:11: cannot resolve symbol
symbol : class Scanner
location: class Test
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
^
2 errors
Tool completed with exit code 1
I have no idea what's going on.
Help is appreciated, Thanks.
Which version of Java are you using? Scanner was added in 1.5.
Run the following command on command prompt (terminal);
java -version
If the returned version number is less than 1.5 then you have to download the new version of Java. Scanner class is not available in prior versions. Download the new version of SDK from here;
Java SE Downloads
After setting up the new version of Java, add the following import statement in your source file;
import java.util.Scanner;
Now compile your source. It should go like a F16 now. But feel free to ask in case of any problem.