PDA

View Full Version : اشکالیابی از کد



BeginnerProgrammer
جمعه 21 تیر 1392, 14:51 عصر
سلام دوستان ، کسی میدونه چرا این کد درست اجرا نمیشه ؟؟؟؟؟؟؟؟؟/


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package file;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
/**
* @author ZARA-T
*/
public class ReadMidlet extends MIDlet implements CommandListener {

private Display display;
private Form form;
private Command read, exit;
StringBuffer buff;

public void startApp(){
display = Display.getDisplay(this);
read = new Command("Read", Command.EXIT, 1);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Read File");
form.addCommand(exit);
form.addCommand(read);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){
notifyDestroyed();
}

public void commandAction(Command c, Displayable s){
if (c==read) {
try {
String SS;
SS=ReadFile("file:///root1//hello.txt");
TextBox input = new TextBox("Enter Some Text:", "", 5, TextField.ANY);
input.setString(SS);
display.setCurrent(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (c==exit) destroyApp(false);

}
private String ReadFile(String url) throws IOException {
FileConnection fc = (FileConnection) Connector.open(url,Connector.READ);
if(fc.exists())
AlertType.ERROR.playSound(display);
StringBuffer sb = new StringBuffer();
try {
InputStream in = fc.openInputStream();
try {
int i;
while ((i = in.read()) != -1) {
sb.append((char) i);
}
} finally {
in.close();
}
} finally {
fc.close();
}
form.append(sb.toString());
return sb.toString();
}
}

spiderman200700
جمعه 21 تیر 1392, 19:11 عصر
مشکل چیه؟
خطایی چیزی؟

BeginnerProgrammer
جمعه 21 تیر 1392, 19:25 عصر
خطا که نداره
فقط من این کدو اجرا میکنم هرچی read میزنم هیچ کاری انجام نمیده فقط روی اون صفحه که میپرسه اجازه ی دستیابی به فایلهای سیستمی میدید میمونه بعدم هرچی yes | no میزنم هیچ اتفاقی نمیفته !!!!!!!!!!!!

BeginnerProgrammer
جمعه 21 تیر 1392, 19:26 عصر
اصلا کارهم به این دو خط کد حتی نمیکشه :

if(fc.exists())
AlertType.ERROR.playSound(display);

از این مرحله به بعد جلو نمیره :::

http://axgig.com/images/25548009291648875721.png

reza_mostafavi63
شنبه 22 تیر 1392, 06:18 صبح
نمیدونم دقیقا میخوای چی کار کنی ولی به نظر میاد میخوای یک فایل رو بخونی من امروز برای خوندن خط به خط یک فایل متنی ، اینترنت رو زیر رو کردم و با کلی تلاش تونستم بلاخره این تابع رو برای این کار بنویسم .با کمی تغیر میتونی برای خوندن فایل باینری هم استفاده کنی .





private void FileSelected(String string) {

string="file:///"+string;
byte buff[]=new byte[1024];
StreamConnection conn ;
InputStream in ;

try{
conn = (StreamConnection) Connector.open(string);
in = conn.openInputStream();
int n;
String line="";
while (true)
{
n=in.read(buff);
if(n>0)
{
for (int i=0;i<n;i++)
if (buff[i]=='\n')
{
FindNewLine(line);
//System.out.println(line);
line="";
}
else
{
if(buff[i]!='\r')
line+=(char)buff[i];
}
}
else
{
if (!line.equals("")) {FindNewLine(line);//System.out.println(line);
}
break;
}
}
conn.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
buff=null;
conn=null;
in=null;

}





FileSelected("root1/New Text Document.txt");

BeginnerProgrammer
شنبه 22 تیر 1392, 17:14 عصر
ممنون از پاسختون ولی من میخوام بدونم اشکال کد کجاست!!!!!!!!!

reza_mostafavi63
دوشنبه 24 تیر 1392, 22:52 عصر
ممنون از پاسختون ولی من میخوام بدونم اشکال کد کجاست!!!!!!!!!

دوست عزیز برنامه رو به صورت زیر تغیر بده :



public void commandAction(Command c, Displayable s){

if (c==read) {

new Thread(new Runnable() {

public void run() {
try {
String SS;
SS=ReadFile("file:///root1//hello.txt");
TextBox input = new TextBox("Enter Some Text:", "", 5, TextField.ANY);
input.setString(SS);display.setCurrent(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
}
if (c==exit) destroyApp(false);
}


با این تغییرات برنامه به درستی کار خواهد کرد . دلیل رو هم خودش در جعبه output (پایین برنامه، همون جایی که ارور ها رو میاره) نشون میده که مربوط به handle میشه. و ما handle عمل خواندن رو به یک thered یا نخ دادیم . فقط دقت کن که فایل hello.txt در آدرس مورد نظر باشه . یعنی داخل root1

ادرس root1 یه چیزی شبیه زیر هست که با جستجو میتونی پیدا کنی
C:\Documents and Settings\USER1\j2mewtk\2.5.2\appdb\DefaultColorPho ne\filesystem\root1

BeginnerProgrammer
دوشنبه 24 تیر 1392, 23:36 عصر
:لبخند: :تشویق: