File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes How to write to a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "How to write to a file?" Watch "How to write to a file?" New topic
Author

How to write to a file?

Soma Sundaram
Greenhorn

Joined: Sep 17, 2006
Posts: 15
Hi
I need help in writing to a file

i have given the sample code

f="mmapi_pic_0000";//file present in root device
ByteArrayOutputStream baos = null;

FileConnection from = (FileConnection)
Connector.open(
"file://" + snpCtrl.getDirectory()+"/"+f+".png");


InputStream fis = from.openInputStream();

int fileSize = (int)from.fileSize();
byte [] buff = new byte[fileSize];

int length = fis.read(buff, 0, fileSize);

baos = new ByteArrayOutputStream(1024);
baos.write(buff, 0, buff.length);
System.out.println(buff.length+f);

imageData = baos.toByteArray();

fis.close();
baos.close();
from.close();

//creating the image from the bytes
Image im = Image.createImage(imageData, 0, imageData.length);

//calling the function
createThumbnail(im);


//the function createThumbnail
private Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();

int thumbWidth = 64;
int thumbHeight = -1;

if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;

Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();

for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy,
Graphics.LEFT | Graphics.TOP);
}
}

Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}

after this the image that is changed should be saved in the root file system by the same name(mmapi_pic_0000).how to do this?
please help me.
pearl view
Greenhorn

Joined: Mar 10, 2007
Posts: 1
hii, this may help you.


This example shows how to write to a file.

/*
J2ME: The Complete Reference

James Keogh

Publisher: McGraw-Hill

ISBN 0072227109

*/
// jad file (Please verify the jar size first)
/*
MIDlet-Name: fileconnection
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: fileconnection.jar
MIDlet-1: fileconnection, , fileconnection
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100

*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class FileConnection extends MIDlet implements CommandListener {
private Command exit, start;
private Display display;
private Form form;
public FileConnection ()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.EXIT, 1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (command == start)
{
try
{
OutputConnection connection = (OutputConnection)
Connector.open("file://c:/myfile.txt;append=true", Connector.WRITE );
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream( out );
output.println( "This is a test." );
out.close();
connection.close();
Alert alert = new Alert("Completed", "Data Written", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( ConnectionNotFoundException error )
{
Alert alert = new Alert(
"Error", "Cannot access file.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( IOException error )
{
Alert alert = new Alert("Error", error.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write to a file?