Reading a file in Blackberry - blackberry-eclipse-plugin

I am trying to read content from file in blackberry and i read the file content to result object and when i am trying to print the result object it gives error and the file is in res folder,and this is code that i got problem
private String readTextFile(String fName) {
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
System.out.println("result data is ...... "+result);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
}

what is fName here ?
The file name or the path could cause similar problem.

Related

How to print from Android Studio Barcode printer

Barcode I want to send a barcode to the printer. I tried it in PDF, JPEG, PNG format. It prints a blank page on all of them.I couldn't find any results on google.
I need to do it without using Printer Helper, if possible
public class yazdır extends AsyncTask<Void , Void,Void> {
#Override
protected Void doInBackground(Void... arg0) {
DataOutputStream outToServer;
Socket clientSocket;
File filename;
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
for (int i=0;i<=adet;i++) {
try {
String path = Environment.getExternalStorageDirectory().toString();
filename = new File(path + "/folder/subfolder/image.png");
FileInputStream fileInputStream = new FileInputStream(filename);
InputStream is = fileInputStream;
clientSocket = new Socket("192.168.1.31", 9100);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
byte[] buffer = new byte[1024];
int size;
while ((size=is.read(buffer)) !=-1){
outToServer.write(buffer,0,size);
}
outToServer.flush();
} catch (ConnectException connectException) {
Log.e(TAG, connectException.toString(), connectException);
return null;
} catch (UnknownHostException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} catch (IOException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} finally {
}
}
return null;}
}
When I send it to a regular printer. Says document is printing and cancels printing.

Error java.lang.IllegalStateException: Unable to obtain OutputStream because Writer is already in use

I'm trying to download an Excel in a liferay portlet but when I'm going to get the outPutPortletStream I obtain the next exception:
java.lang.IllegalStateException: Unable to obtain OutputStream because Writer is already in use
This is the method I'm using to download the Excel:
public static void descargaFichero(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {
HttpServletRequest request = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(resourceRequest));
OutputStreamWriter osWriter = null;
BufferedWriter bWriter = null;
OutputStream outputStream = null;
InputStream is = null;
InputStreamReader isr = null;
File tempFile = null;
try {
String idDescarga = request.getParameter("id-descarga");
String rutaFicheroDescarga = Encriptador.decrypt(ConstantesFlujos.CLAVE_ENCRIPTADO_JSON, idDescarga);
String extension = FileUtil.getExtension(rutaFicheroDescarga);
String nombreArchivo = String.format("%s%s.%s",
ConstantesFlujos.PREFIJO_NOMBRE_FICHERO_RENOVACION,
fechaDesglosada(),
extension);
if(GestorLog.isInfoEnabled(clase)){
GestorLog.info(clase, String.format("Descargando fichero temporal: '%s' a '%s'", rutaFicheroDescarga, nombreArchivo));
}
if (extension.equalsIgnoreCase("xls")) {
resourceResponse.setContentType("application/vnd.ms-excel");
} else {
resourceResponse.setContentType("application/octet-stream");
}
((PortletResponse) resourceResponse).addProperty("Content-disposition", String.format("atachment; filename=%s", nombreArchivo));
tempFile = new File(rutaFicheroDescarga);
outputStream = resourceResponse.getPortletOutputStream();
osWriter = new OutputStreamWriter(outputStream, "ISO-8859-1");
char[] buf = new char[8192];
is = new FileInputStream(tempFile);
isr = new InputStreamReader(is, "ISO-8859-1");
int c = 0;
while ((c = isr.read(buf, 0, buf.length)) > 0) {
osWriter.write(buf, 0, c);
osWriter.flush();
}
} catch(Exception e){
GestorLog.error(clase, "Error en el metodo descargaFichero: " + e.getMessage());
throw e;
} finally {
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
if (bWriter != null) {
bWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (tempFile != null) {
tempFile.delete();
}
}
}
And I get the error in the line:
outputStream = resourceResponse.getPortletOutputStream();
This might be due to resourceResponse.addProperty being called before accessing the stream. Those operations can be nasty, as two parties will now try to write data to the output.
An easy way to omit all problems is to just delegate it all to the portal: PortletResponseUtil.sendFile exists in a couple of different variations, e.g.
PortletResponseUtil.sendFile(
resourceRequest,
resourceResponse,
fileName,
inputStream,
contentLength,
contentType,
contentDispositionType); // e.g. HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT

System.out.println displays blank line

Why does the below 'System.out.println'(fileEncodedString) display a blank line while the value is there when I debug and I do get an output when I decode the value and print.
byte[] fileContent = readFileToByteArray(filePath);
String fileEncodedString = Base64.getEncoder().encodeToString(fileContent);
byte[] fileDecodedString = Base64.getDecoder().decode(fileEncodedString);
System.out.println("Base64 Decoded File (Basic): " + new String(fileDecodedString, "utf-8"));
private static byte[] readFileToByteArray(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
// read file into bytes[]
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
Try to use below method for readFileToByteArray and pass correct file path:
static byte[] readFileToByteArray(String filePath) throws IOException {
File file = new File(filePath);
return Files.readAllBytes(file.toPath());
}
its working fine.

FileInputStream issue - App has stopped working

I am having trouble with FileInputStream in Android Studio. My application has stopped working when I use it to read file .txt in the internal memory (I have writen data to this file successfully). I have try many solutions but they didn't work. Can anyone give the solution for this problem? Here is my code. Thanks very much. (I have added users-permission "WRITE_EXTERNAL_STORAGE" and "READ_EXTERNAL_STORAGE").
String fileName = "infoProfile.txt";
private int readData() {
try {
String path = MainActivity.this.getFilesDir().getAbsolutePath() + "/" + fileName;
FileInputStream in = openFileInput(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
ArrayList<String> str = new ArrayList<>();
String data = "";
while ((data = reader.readLine()) != null) {
str.add(data);
}
in.close();
if (str.isEmpty())
return 0;
etName.setText(str.get(0));
etEmail.setText(str.get(1));
etPhone.setText(str.get(2));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}

how to store the data in a file in j2me..?

please can anyone tell me how to store the RMS data to file and how to modify it?
You can use this code for write the data in a file.
private void writeTextFile(String fName, String text) {
DataOutputStream dos = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();
dos = fconn.openDataOutputStream();
dos.write(text.getBytes());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (dos != null)
dos.close();
if (fconn != null)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

Resources