How can I index first line from files in a field and other lines in a different field?
My code is:
FileInputStream fis;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
return;
}
try {
Document doc = new Document();
doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))));
} finally {
fis.close();
}
Please help me!
I did it!
FileInputStream fis;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
return;
}
try {
Document doc = new Document();
String line = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))) {
line = reader.readLine();
Field headerField = new TextField("header", line, Field.Store.YES);
headerField.setBoost(2.0F);
doc.add(headerField);
while ((line = reader.readLine()) != null ) {
doc.add(new TextField("contents", line, Field.Store.YES));
}
} catch (IOException e) {
System.err.println(e);
}
} finally {
fis.close();
}
Related
Below I have the following code to read in a file and go through it line by line.. This is using java's BufferedReader class. That I am fine with.
String filename = "C:\\test.txt"
String line = null;
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
while (((line = bufferedReader.readLine()) != null)) {
//do the following....
}
} catch (IOException) {
e.printStackTrace();
}
However I want to now start using InputStreamReader in Spring / Java. I have the below code written but I am unsure how I can step through my file line by line. Really confused over this part. Anyone have any ideas or know how this can be done?
String filepath= "C:\\test.txt"
File filename= new File(filepath);
try {
InputStream fileInputStream = new BOMInputStream(new fileInputStream(filename));
// now want to step through the file, line by line..
} catch (IOException) {
e.printStackTrace();
}
Thanks
This is how you can read your input file byte by byte using InputStreamReader.
char[] chars = new char[100];
try {
InputStream inputStream = new FileInputStream("C:\\test.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
inputStreamReader.read(chars);
System.out.println(new String(chars).trim());
} catch (IOException e) {
e.printStackTrace();
}
Check this out -
String filename = "C:\\test.txt"
String line = null;
FileInputStream fileInputStream = new FileInputStream(filename);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
try {
while (((line = bufferedReader.readLine()) != null)) {
//do the following....
}
} catch (IOException) {
e.printStackTrace();
}
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\test.txt")))) {
reader.lines().forEach(line -> {
// do what you want with the line
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
I am trying to read a text file and save each line of text into an ArrayList. I have tried various methods, including FileInputStream and BufferedReader. Here is the code that currently gets me the closest to what I am trying to do
try {
InputStream is = getResources().openRawResource(R.File.txt);
BufferedReader bufferedReader = new BufferedReader(new FileReader("File.txt"));
String line;
while((line = bufferedReader.readLine()) != null)
{
allText.add(line);
}
bufferedReader.close();
}
catch(IOException e)
{
}
allText is an ArrayList previously instantiated. Right now the file is saved in /res and I get an "invalid resource directory warning". I would like to know where to save the file properly and how to read from it.
The line should be
InputStream is = getResources().openRawResource(R.File.txt);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
You have made an InputStream for resource file and use BufferedReader to read from the stream created.
Reading from /assets folder use getAssets() method
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("File.txt"), "UTF-8"));
String myData = reader.readLine();
while (myData != null) {
myData = reader.readLine();
}
} catch (IOException e) {
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
Reading file from /res/raw folder
InputStream fileInputStream = getResources().openRawResource(R.raw.File);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = fileInputStream .read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
fileInputStream .close();
} catch (IOException e) {
}
return outputStream.toString();
}
At the moment i'm trying to save a response to the internal storage in the phone. Everything works fine up until i try and retrieve the data again. When i log out the retrieved data it only logs out one small section of the response and the rest isn't there. Ive tried deleting the file and calling it again just incase it was using an old one.
Saving Code
try {
String response = apiResponse.getRawResponse();
Log.e("Response", response);
FileOutputStream userInfo = openFileOutput("personal_profile", MODE_PRIVATE);
userInfo.write(response.getBytes());
userInfo.close();
} catch (Exception e) {
e.printStackTrace();
Retrieving Code
String response = "";
try {
FileInputStream fis = getActivity().openFileInput("personal_profile");
DataInputStream isr = new DataInputStream(fis);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(isr));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
line = response;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Saved File", response);
Any kind of suggestions would be great!
REASON
The problem was that the line variable is assigned again in every iteration
Try this:
String response = "";
try {
FileInputStream fis = getActivity().openFileInput("personal_profile");
DataInputStream isr = new DataInputStream(fis);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(isr));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
line = response;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
CHANGE LAST LINE
Log.e("Saved File", sb.toString());
Have you got this in your AndroidManifest.xml file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also, this link has everything you need to know about reading and writing files:
http://www.anddev.org/working_with_files-t115.html
Code::
String response = "";
try {
FileInputStream fis = getActivity().openFileInput("personal_profile");
DataInputStream isr = new DataInputStream(fis);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(isr));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
line = response;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Saved File", sb.toString());
I am using JTidy to convert from HTML to XHTML but I found in my XHTML file this tag .
Can i prevent it ?
this is my code
//from html to xhtml
try
{
fis = new FileInputStream(htmlFileName);
}
catch (java.io.FileNotFoundException e)
{
System.out.println("File not found: " + htmlFileName);
}
Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXmlTags(false);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);//
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(fis, null);
try
{
tidy.pprint(xmlDoc,new FileOutputStream("c.xhtml"));
}
catch(Exception e)
{
}
I had only success, when the input is treated as XML as well. So either set xmltags to true
tidy.setXmlTags(true);
and live with the errors and warnings or do the conversion twice.
First conversion to sanitize the html (html to xhtml) and a second conversion from xhtml to xhtml with set xmltags, thus no errors and warnings occur.
String htmlFileName = "test.html";
try( InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(htmlFileName);
FileOutputStream fos = new FileOutputStream("tmp.xhtml");) {
Tidy tidy = new Tidy();
tidy.setShowWarnings(true);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(in, fos);
} catch (Exception e) {
e.printStackTrace();
}
try( InputStream in = new FileInputStream("tmp.xhtml");
FileOutputStream fos = new FileOutputStream("c.xhtml");) {
Tidy tidy = new Tidy();
tidy.setShowWarnings(true);
tidy.setXmlTags(true);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(in, null);
tidy.pprint(xmlDoc, fos);
} catch (Exception e) {
e.printStackTrace();
}
I used the latest jtidy version 938.
i created a function that parse the the xhtml code and remove the unwelcome tags
and to add a link to the css File "tableStyle.css"
public static String xhtmlparser(){
String Cleanline="";
try {
// the file url
FileInputStream fstream = new FileInputStream("c.xhtml");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine = null;
int linescounter=0;
while ((strLine = br.readLine()) != null) {// read every line in the file
String m=strLine.replaceAll(" ", "");
linescounter++;
if(linescounter==5)
m=m+"\n"+ "<link rel="+ "\"stylesheet\" "+"type="+ "\"text/css\" "+"href= " +"\"tableStyle.css\""+ "/>";
Cleanline+=m+"\n";
}
}
catch(IOException e){}
return Cleanline;
}
but as a performance issue is it good?
by the way it works will
You can use the following method to get xhtml from html
public static String getXHTMLFromHTML(String inputFile,
String outputFile) throws Exception {
File file = new File(inputFile);
FileOutputStream fos = null;
InputStream is = null;
try {
fos = new FileOutputStream(outputFile);
is = new FileInputStream(file);
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.parse(is, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
fos = null;
}
fos = null;
}
if(is != null){
try {
is.close();
} catch (IOException e) {
is = null;
}
is = null;
}
}
return outputFile;
}
i want to append a text in an existing file but i can not read it(i can read the first inserted data) i do not know what is the mistake .
this is the write code(save in file):
FileOutputStream fos = openFileOutput("test",MODE_APPEND);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(text);
oos.flush();
oos.close();
and this is how to read(read the data from the file):
FileInputStream fis = openFileInput("test");
ObjectInputStream ois = new ObjectInputStream(fis);
String s=(String) ois.readObject();
while(s != null){
Toast.makeText(getApplicationContext(),s, Toast.LENGTH_SHORT).show();
s=(String) ois.readObject();
Toast.makeText(getApplicationContext(),s, Toast.LENGTH_SHORT).show();
}
pleas help me !! are there a wrong in writing or in the reading code
public boolean writeToFile(String filename,String data){
try {
FileOutputStream fos = openFileOutput(filename,0);
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(filename,0));
out.write(data);
out.close();
return true;
} catch (java.io.IOException e) {
e.printStackTrace();
System.out.println("-----problem in writeToFile()--------");
return false;
}
}
public String readFromFile(String xxx){
StringBuffer returnString = new StringBuffer(""); ;
try{
FileInputStream fstream = openFileInput(xxx);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
returnString.append(strLine);
}
in.close();
}catch (Exception e){//Catch exception if any
e.printStackTrace();
System.out.println("-----problem in readFromFile()--------");
}
return returnString.toString();
}