I am trying to convert ansii(latin-5) text file to utf-8 text file in a directory. I made a small mechanish to understand if the file is ansii or utf-8 however when i try to change ansii file to utf-8 program deletes all values in the text. Where am i doing wrong?
Thanks in advance.
Here is my code:
package altyazi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
public class operation{
public static int howmany =0;
public static int howmanysmalli=0;
public static double ratio;
File myFile;
public static void koddegıstır(String myfile) throws IOException{
File file = new File(myfile);
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray);
fis.close();
int[] freqs = new int[256];
for(byte b: bytesArray){
freqs[b&0x0ff]++;
}
howmany = freqs[107]+freqs[75];
howmanysmalli=freqs[253];
System.out.println("Character \"k\" appears " + howmany +" times in the text "+myfile);
ratio = (double)howmany/(double)bytesArray.length;
System.out.println("How many: "+howmany);
System.out.println("Length: "+bytesArray.length);
System.out.println("Ratio: "+ratio);
//Cp1254
if(ratio<0.01){
System.out.println("Text file is probably not turkish");
}else{
System.out.println("Text file is probably turkish");
if(howmanysmalli>20){
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(myfile),
"ISO-8859-9"));
Writer out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(
myfile), "UTF-8"));
try {
while ((line = br.readLine()) != null) {
out.write(line);
out.write("\n");
}
} finally {
br.close();
out.close();
}
}else{
System.out.println("Passed as utf-8");
}
}
}
}
You are overwriting the file when you create the FileOutputStream. This creates an empty file. You need to write to a new file, and delete the old one and rename the new one when complete.
Related
On renaming and moving a file using java code,the contents of the file is removed.How to rename and move a file using java code without erasing the content
Please use below code, it can rename and copy the file into different folder
package han.code.development;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileRenameandMove
{
public static void main(String[] args) throws IOException
{
File file=new File("D:\\Haneef\\Stackoverflow\\files.txt");
FileReader fileR=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileR);
String str=bufferedReader.readLine();
FileWriter fileW=new FileWriter(new File("D:\\Haneef\\Stackoverflow\\Move\\File1.txt"));
fileW.write(str);
fileW.close();
}
}
try{
File afile =new File("C:\\Users\\Jen\\Downloads\\usage.csv");
File bfile =new File("C:\\Users\\Jen\\Desktop\\BJ\\");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile + "\\"+ dd.get(j).getText()+ "_March _2018.csv");
Thread.sleep(1000);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
// if file copied successfully then delete the original file
afile.delete();
System.out.println("File moved successfully");
}catch(IOException e){
e.printStackTrace();
}
}
I am out of ideas, I've been trying for the whole day to separate one file which has a format of :
AN Aixas
AN Aixirivall
AN Aixovall
AN Andorra la Vella
BR Salto do Mandira
BR Salto do Norte
BR Salto Dollman
BR Salto Grande
BR Salto Pilao
...
and so one, into different files by the name of the Country "AA.txt" and to include all the cities in these separate files. But my program only writes to a certain bunch of files and I cannot figure out why.
I've tried all the writing files classes - same result.
Here is the result, all worked but on a certain bunch of files only.
Here is the code :
package com.fileorganizer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class File2 implements Cloneable {
static InputStream fis = null;
static BufferedReader br = null;
static String state = "";
static String tmp = "";
static File file = null;
static FileWriter fw = null;
static BufferedWriter bw = null;
public static void main(String[] args) {
int a = 0;
try {
fis = new FileInputStream(
new File(
"/Users/Mihail/Documents/WorkSpace/Parse-Starter-Project-1.8.2/ParseStarterProject/res/raw/cities.txt"));
br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
state = line.substring(0, 2);
if (state.substring(0, 1).matches("^[A-Z]+$")
&& state.substring(1, 2).matches("^[A-Z]+$")
&& !tmp.equals(state)) {
file = new File(
"/Users/Mihail/Documents/WorkSpace/Parse-Starter-Project-1.8.2/ParseStarterProject/res/raw/countriesFolder/"
+ state + ".txt");
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
tmp = state;
}
bw.write(line.substring(3) + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (Exception ex) {
}
}
}
}
I am really sorry for such a dumb question. Please help
You don't close bw anywhere, so the contents in the BufferedWriter's buffer are lost.
I have written a simple code to write text into file but i am not able to figure out why text is not written to file. I have written a class with method which will take text file as input and create the text file if it is not exist
here is the code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class OutputFile {
public static BufferedWriter fbw = null;
public BufferedWriter createFile(String text)
{
try{
File file =new File(text);
if(!file.exists()){
file.createNewFile();
}
FileWriter writer = new FileWriter(text,true);
fbw =new BufferedWriter(writer);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return fbw;
}
public static void main(String args[]) throws IOException
{
String resultFilePath = "C:/Users/Desktop/stringcompare/output.txt";
OutputFile file = new OutputFile();
fbw = file.createFile(resultFilePath);
fbw.write("hello");
fbw.newLine();
fbw.close();
}
}
Try this:
try(BufferedWriter bw = new BufferedWriter(new FileWriter("FILE_PATH_HERE"))){
br.write("STUFF_TO_WRITE_HERE");
} catch(IOException e){
}
With this try-with-resources statement, all of the closing file effort is done automatically.
This will only work for Java 7 or higher.
You have to flush your output. This can be done directly by calling writer.flush().
When trying to create two methods for counting the no. of rows and reading the values of a file, only one of these methods got executed and another is not executed showing the following error :Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Read error
Please look at the following code:
package com.ibm.csvreader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class CsvFileReader2 {
public static class opencsvfile {
HashMap <String , String> map= new HashMap <String, String> ();
//csv file containing data
// FileReader strFile = new FileReader("C:/Users/vmuser/Desktop/SampleUpload.csv");
//create BufferedReader to read csv file
// BufferedReader br = new BufferedReader((strFile));
String strLine = "";
int lineNumber ;
public void countrows(FileInputStream fstream) throws Exception{
DataInputStream strFile = new DataInputStream(fstream);
BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
lineNumber =0;
while( (strLine = br.readLine()) != null) {
lineNumber++;
}
System.out.println("no.of rows are :" +lineNumber);
br.close();
}
public void readfile(FileInputStream fstream) throws Exception{
DataInputStream strFile = new DataInputStream(fstream);
BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
lineNumber =0;
while( (strLine = br.readLine()) != null) {
lineNumber++;
String[] tokens = strLine.split(",");
String key = tokens[0].trim();
String nodes = tokens[1].trim();
map.put(key, nodes);
}
System.out.println("map is" + map );
br.close();
System.out.println("File is Closed");
}
}
public static void main(String[] args) throws IOException {
File fl = new File ("C:/Users/vmuser/Desktop/SampleUpload.csv");
FileInputStream fstream = new FileInputStream(fl);
opencsvfile f=new opencsvfile();
try {
f.countrows(fstream);
f.readfile(fstream);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Just a small modification will do the work:
package com.ibm.csvreader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class CsvFileReader2 {
public static class opencsvfile {
HashMap <String , String> map= new HashMap <String, String> ();
//csv file containing data
// FileReader strFile = new FileReader("C:/Users/vmuser/Desktop/SampleUpload.csv");
//create BufferedReader to read csv file
// BufferedReader br = new BufferedReader((strFile));
String strLine = "";
int lineNumber ;
public void countrows(FileInputStream fstream) throws Exception{
DataInputStream strFile = new DataInputStream(fstream);
BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
lineNumber =0;
while( (strLine = br.readLine()) != null) {
lineNumber++;
}
System.out.println("no.of rows are :" +lineNumber);
br.close();
}
public void readfile(FileInputStream fstream) throws Exception{
DataInputStream strFile = new DataInputStream(fstream);
BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
lineNumber =0;
while( (strLine = br.readLine()) != null) {
lineNumber++;
String[] tokens = strLine.split(",");
String key = tokens[0].trim();
String nodes = tokens[1].trim();
map.put(key, nodes);
}
System.out.println("map is" + map );
br.close();
System.out.println("File is Closed");
}
}
public static void main(String[] args) throws IOException {
File fl = new File ("C:/Users/vmuser/Desktop/SampleUpload.csv");
FileInputStream fstream = new FileInputStream(fl);
opencsvfile f=new opencsvfile();
try {
f.countrows(fstream);
fstream = new FileInputStream(fl);//include this line
f.readfile(fstream);
} catch (Exception e) {
throw new RuntimeException(e);
}
finally{
if(fstream!=null)
fstream.close();//be sure to close all streams at last
}
}
}
Close all other streams as well. Above code will work for you.Cheers.
When you close your BufferedReader, it also closes the nested classes, including the FileInputStream.
Instead of closing it, you should try and reset() it to restart reading it from the start.
Or you must re-open the FileInputStream.
I am trying to read in a technical paper, separate all the sentences, use a filter to find key terms and phrases in the sentences, and then create my own abstract.
What I have so far is two BufferedReaders reading a text file with a paragraph in it, and my filter being read in. Each line is then being stored into an ArrayList and printed to the console to test if they are being read correctly.
I want to know if I am approaching this the correct way by using a BufferedReader instead of a Scanner. I just want to be able to print out all the sentences after a '.' (dot), a '!' (exclamation-point), or a '?' (question-mark) for right now, so I know that the file is being read correctly.
This is my code so far:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Filtering {
public static void main(String[] args) throws IOException {
ArrayList<String> lines1 = new ArrayList<String>();
ArrayList<String> lines2 = new ArrayList<String>();
try {
FileInputStream fstream1 = new FileInputStream("paper.txt");
FileInputStream fstream2 = new FileInputStream("filter2.txt");
DataInputStream inStream1 = new DataInputStream (fstream1);
DataInputStream inStream2 = new DataInputStream (fstream2);
BufferedReader br1 = new BufferedReader(
new InputStreamReader(inStream1));
BufferedReader br2 = new BufferedReader(
new InputStreamReader(inStream2));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
lines1.add(strLine1);
}
while ((strLine2 = br2.readLine()) != null) {
lines2.add(strLine2);
}
inStream1.close();
inStream2.close();
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(lines1);
System.out.println(lines2);
}
}
It is a good practice to use a BufferedReader to read any File as it will buffer the File instead of accessing each bytes one by one
The DataInputStream is not needed
You should specify a character encoding in your InputStreamReader
You could accumulate all your string in a StringBuilder so that you have the whole text in a single reference
You may want to look into BreakIterator to split your text into sentences. Have a look at getSentenceInstance().
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
public class Filtering {
public static void main(String[] args) throws IOException {
File paperFile = new File("paper.txt");
File filterFile = new File("filter2.txt");
// If you want you could roughly initiate the stringbuilders to their
// approximate future size
StringBuilder paper = new StringBuilder();
StringBuilder filter2 = new StringBuilder();
FileInputStream fstream1 = null;
FileInputStream fstream2 = null;
try {
fstream1 = new FileInputStream(paperFile);
fstream2 = new FileInputStream(filterFile);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fstream1, "UTF-8"));
BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2, "UTF-8"));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
paper.append(strLine1).append('\n');
}
while ((strLine2 = br2.readLine()) != null) {
filter2.append(strLine2).append('\n');
}
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (fstream1 != null) {
fstream1.close();
}
if (fstream2 != null) {
fstream2.close();
}
}
String paperString = paper.toString();
String filterString = filter2.toString();
System.out.println(paperString);
System.out.println(filterString);
// To break it into sentences
BreakIterator boundary = BreakIterator.getSentenceInstance();
boundary.setText(paperString);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
System.out.println(paper.substring(start, end));
}
}
}