Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java Micro Edition
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java Micro Edition
read line by line from file
mahdi farzami
Ranch Hand
Posts: 32
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi , how can i read a file line by line ???
i want to save each line in slot of
String
array
i write this code but it`s return all of file !!!
please help me to change this code
thanks
import java.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Midlet extends MIDlet implements CommandListener { private Display display; private Form form; private Command read, exit; private Alert alert; private String[] db; private int i = 0; public Midlet() { display = Display.getDisplay(this); read = new Command("Read", Command.SCREEN, 1); exit = new Command("Exit", Command.EXIT, 1); form = new Form("Read File"); form.addCommand(exit); form.addCommand(read); form.setCommandListener(this); } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); } public void commandAction(Command c, Displayable s) { String label = c.getLabel(); if (label.equals("Read")) { String stText = readUnicodeFile("test.txt", "UTF-8"); if (stText != null) { alert = new Alert("Reading", stText, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert, form); } } else if (label.equals("Exit")) { destroyApp(false); } } /* private String file(){ InputStream is = getClass().getResourceAsStream("test.txt"); StringBuffer sb = new StringBuffer(); try{ int chars, i = 0; while ((chars = is.read()) != -1){ sb.append((char) chars); } String stText=readUnicodeFile("test.txt","UTF-8"); return sb.toString(); }catch (Exception e){} return null; }*/ private String readUnicodeFile(String string, String string0) { StringBuffer buffer = new StringBuffer(); java.io.InputStream is = null; java.io.InputStreamReader isr = null; try { int ch = 0; Class c = this.getClass(); is = c.getResourceAsStream(string); isr = new InputStreamReader(is, string0); while ((ch = isr.read()) > -1) { buffer.append((char) ch); } isr.close(); if (is != null) { is.close(); } } catch (Exception ex) { ex.printStackTrace(); } return buffer.toString(); } }
Gopinath Karyadath
Ranch Hand
Posts: 87
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Dear friend
String des=""
Vector vector = new Vector ();
while ((ch = isr.read()) > -1) {
buffer.append( (char) ch ) ;
des+= (String) (char) ch ;
if ( (char) ch == '\n' ) {
vector.addelements(des) ;
des = "" ;
}
}
regards
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Can Someone Help with Reading from a text file in J2ME
How to send bytearray from Servlet to Midlet application?
HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS
How to write to a file?
Writing from a midlet to a file
More...