Read cyrillics and non-cyrillics characters from file java - java

I have a file with cyrillics and non-cyrillics characters. However, when I read the file the cyrillics characters are not retrived and non-cyrillics characters are retrived. Here is the code I am using
private static String dirToPRocess = "D:\\stopwords_freq_v2.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
dirToPRocess), "UTF-8"));
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Are you using eclipse?
You can try the following to get it to work:
save your java file with the character encoding utf-8.
If you want to print cyrillics to the console, I think there might be a setting in eclipse's properties somewhere that does that but not 100% certain- it should print cyrillics by default in my experience.
Your java code looks OK btw.

Related

JAVA Rewriting in a file

I am trying to rewrite in a file, alongside with the other text in it(not overwrite). But i don't know how can i do it using the exception as my buffered reader lose his initialization.
BufferedReader br;
try {
br = new BufferedReader(new FileReader("file.txt"));
String line ;
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter bw;
try {
File file = new File("file.txt");
bw = new BufferedWriter(new FileWriter("file.txt"));
if(br.readLine() != null) //ERROR Local variable may have not been initialized
bw.newLine();
bw.write("Hello");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileWriter has the ability to set the append flag new FileWriter(file, append)
or if you want to use your version
Initialize the variable with null
BufferedReader br = null;
try with resource may be useful here as well, because it closes everything for you automatically.
Simple. Initialize the variable to null.
Say first line as,
BufferedReader br = null;
...
try {
File file = new File("file.txt");
bw = new BufferedWriter(new FileWriter("file.txt",true));
if(br.readLine() != null) //ERROR Local variable may have not been initialized
...
Try that on for size. That "true" after file.txt means it will append to what file you've found.
Here's a link to the documentation. Take note of the last constructor for FileWriter.
BufferedReader br = null;
To fix your current error changing your first line to this would be how you do it. That reader however is not needed whatsoever.

How to write in specific place inside a file using ant

i am using java ant to deploy my application . I have a file app.php . I want to write some text in app.php while deploying in a specific location inside that file . This is my app.php :
'providers' => array(
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
),
I want to add a line at the end of this line :
'Illuminate\Workbench\WorkbenchServiceProvider',
Please tell me how to do this .
Thanks.
you must use subString method like this:
at first store your file in a String so after that you could do this:
String s="your file";
String firstPart=s.substring(0,s.lastIndexOf(")")+1);
String lastPart=s.substring(s.lastIndexOf(")")+1);
firstPart=firstPart+"\n"+"'Illuminate\\Workbench\\WorkbenchServiceProvider',";
s=firstPart+lastPart;
How to read from file ?
Use BufferedReader to wrap a FileReader
BufferedReader br = null;
StringBuilder sb=new StringBuilder();
try {
String line;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((line= br.readLine()) != null) {
sb.append(line+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String str=sb.toString();
See the Replace or Filter ant tasks.

Open text file from with JAR without extracting it - Help please

I am trying to open a .txt file saved within a JAR and display its contents in a JTextArea. Below is the code I am trying to use;
URL urlToDictionary = this.getClass().getResource("eula/" + "eula.txt");
try {
InputStream stream = urlToDictionary.openStream();
gettysburgTextStrBlder = stream;
System.out.println(stream);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
I know I am in the correct file location as I have changed the .getResource path around and seen null point exceptions, I have none with the current file path.
The System.out prints the following at runtime:
java.io.BufferedInputStream#3af42ad0
I have also tried;
gettysburgTextStrBlder = String.valueOf(stream);
But the result I get is the same.
I think I am nearly there, but am unsure how to get the actual content of the .txt file and not just the Buffered stream.
Thanks.
Andy
You have to read the content from the inputstream and display in the text area using BufferedReader
URL urlToDictionary = this.getClass().getResource("eula/" + "eula.txt");
try {
InputStream stream = urlToDictionary.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = null;
StringBuffer lineContent = new StringBuffer();
while((line = br.readLine()) != null){
lineContent.append(line).append("\n");
}
br.close().
System.out.println(lineContent.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Efficiency while reading a txt file to an array

I have written a script in which I read in .txt files to arrays. It works perfectly but I'm curious if there is a more efficient way of reading the .txt files in.
I have this:
try {
String line;
filestart = new BufferedReader(new FileReader("C:\\path\\file.txt));
for (line = filestart.readLine(); line !=null; line = filestart.readLine()) {
array.add (line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Is there a function that combines BufferedReader and FileReader?
How would Scanner compare with what I have?

First letter missing when reading a file

I am retrieving data from a file and for some reason i miss the first char every time.
my code.
public String readFile(){
String str = "Not Authenticated";
//Reading the file
try{
FileInputStream fIn = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[isr.read()]; //str.length()
// Fill the Buffer with data from the file
try {
isr.read(inputBuffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
// Transform the chars to a String
String readString = new String(inputBuffer);
str = readString;
} catch (IOException ioe)
{return ioe.toString();}
return str;
}
the file contains the word "True"
i get "rue"
also when i create the file the first letter cannot be a capital? if i use a capital the file is never found i am guessing the two are not related.
If that file is text file then read it via BufferedReader.
StringBuilder sb=new StringBuilder();
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(openFileInput(fileName));
String line=null;
while((line = br.readLine()) != null)
{
sb.append(line);
}
}catch(Exception ex){
//
}finally{
if(br!=null)
br.close();
}
return sb.toString();
char[] inputBuffer = new char[isr.read()]; //str.length()
Does this not read a character out of your reader?
EDIT: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStreamReader.html
isr.read() will read a single character (ie. the first character).
To get the size of the file, you can use
long length = new File(fileName).length()
See File.length() function for details.
You can use File class to find length of a file,:
File f=new File("c:\\new\\abc.txt");
f.length(); will return size in bytes
You should also close the file opened. by isr.close();

Categories

Resources