我找到了许多创建输出流的示例,并在我的代码中使用了它们。我收到以下错误:不兼容的类型:java.lang.String 无法转换为 java.io.OutputStream 在我度过这个阶段之前,我无法进入下一阶段。任何想法,将不胜感激。 这是课程代码:
import static com.codename1.ui.CN.*;
import com.codename1.components.ScaleImageLabel;
import com.codename1.system.Lifecycle;
import com.codename1.ui.*;
import com.codename1.ui.layouts.*;
import com.codename1.io.*;
import com.codename1.ui.plaf.*;
import com.codename1.ui.util.Resources;
import org.w3c.dom.Text;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.lang.Object;
import java.io.InputStream;
import com.codename1.ui.CN.*;
import com.codename1.io.File;
import com.codename1.util.StringUtil;
import java.util.ArrayList;
import java.io.Writer;
import java.io.StringWriter;
public class Plays {
Form plays = new Form(new FlowLayout());
ArrayList PlayList = new ArrayList();
FileSystemStorage fs = FileSystemStorage.getInstance();
public Plays() throws IOException {
try {
InputStream fis = fs.openInputStream("plays.txt");
} catch (Exception ex) {
Log.e(ex);
Dialog.show("Touchline Assitant", "I cannot find the Plays List!", "OK", null);
}
String fileName = fs.getAppHomePath() + "plays.txt";
try(Writer w = new OutputStreamWriter(fileName)) {
w.write( "Play List");
}
plays.show();
}
}
代码中有几个错误。这一行:
InputStream fis = fs.openInputStream("plays.txt");
使用相对路径而不是绝对路径。应该是:
InputStream fis = fs.openInputStream(fileName);
但是更好的方法是:
if(fs.exists(fileName)) {
... load the file
}
要打开文件流,您需要使用
fs
方法。具体来说:
new OutputStreamWriter(fs.openOutputStream(fileName))