首先,我需要在内部存储中创建文本文件(如果文件不存在),然后逐行读取值到数组中。我不明白我做错了什么,为什么我从内存中读取的内容并没有检测到我的文件,我应该如何解决? (我想创建文件并写入它是正确的)
我希望我的数据文件如何:
data
data
data
data
data
data
我如何将值写入文件:
FileOutputStream fOut = null;
try {
fOut = openFileOutput("VilniusWeatherVU.txt",MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String v33 = "data";
String v12 = "data";
String v3 = "data";
String v11 = "data";
String v6 = "data";
String v1 = "data";
try {
fOut.write(v33.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.write(v12.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.write(v3.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.write(v11.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.write(v6.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.write(v1.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
我如何阅读价值观:
String[] array = new String[6];
int index = 0;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("VilniusWeatherVU.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//System.out.println (strLine);
array[index]=strLine;
index++;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
我如何检查我的阵列:
for (int i = 0; i <6; i++){
Log.v(LOG_TAG, "CHECK IF IT WORKED " + array[i]);
}
我的logcat说:
11-24 16:06:56.886 359-359/app.sunshine.android.example.com.sunshine W/System.err﹕ Error: /VilniusWeatherVU.txt (No such file or directory)
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
11-24 16:06:56.895 359-359/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED null
请不要让我独自一人:(
Frunk要求:
我试图以不同的方式从文件中读取:
FileInputStream fin = null;
try {
fin = openFileInput("VilniusWeatherVU.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int c;
String temp="";
try {
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
} catch (IOException e) {
e.printStackTrace();
}
//string temp contains all the data of the file.
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
然后我用logcat来检查:
Log.v(LOG_TAG, "CHECK IF IT WORKED " + temp);
我得到的是:
11-24 16:24:38.345 334-334/app.sunshine.android.example.com.sunshine V/MyNewMain﹕ CHECK IF IT WORKED datadatadatadatadatadata
答案 0 :(得分:1)
尝试使用openFileInput(&#34; VilniusWeatherVU.txt&#34;)来读取你的文件txt。
例如:
FileInputStream fileInputStream= openFileInput("VilniusWeatherVU.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder= new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
答案 1 :(得分:1)
public class MyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.myfragment, container, false);
writeFile("text.txt", view.getContext());
String[] test = readFile("text.txt", view.getContext());
if(test==null)
{
// error to read file or file dont exists
}else
{
//read file
}
return view;
}
public static String newline = System.getProperty("line.separator");
// write file
private void writeFile(String filename, Context context)
{
File file = new File(context.getFilesDir(), filename);
// write data if file dont exists
if (!file.exists())
{
String string = "data" + newline + "data" + newline + "data"
+ newline + "data" + newline + "data" + newline + "data"
+ newline;
FileOutputStream outputStream;
try
{
outputStream = context.openFileOutput(filename,
Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
// read file and convert data to String array
private String[] readFile(String filename, Context context)
{
try
{
File file = new File(context.getFilesDir(), filename);
if (!file.exists())
{
return null;
}
FileInputStream fis = new FileInputStream(file);
byte[] dataByte = new byte[(int) file.length()];
fis.read(dataByte);
fis.close();
String data = new String(dataByte);
// slit string with newline <\n>
return data.split(newline);
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
答案 2 :(得分:0)
fOut = openFileOutput("VilniusWeatherVU.txt",MODE_WORLD_READABLE);
与
不符FileInputStream fstream = new FileInputStream("VilniusWeatherVU.txt");
更好:要阅读文件,请使用getFilesDir()
作为存储文件的目录的完整路径。