gpt4 book ai didi

java - 将文件读入数组后android NULL值

转载 作者:行者123 更新时间:2023-12-02 05:14:15 25 4
gpt4 key购买 nike

首先,我需要在内部存储中创建文本文件(如果文件不存在),然后将值逐行读取到数组中。我不明白我做错了什么,为什么我从内存中读取的内容没有检测到我的文件,我应该如何解决这个问题? (我认为创建文件并写入它是正确的)

我希望我的数据文件是什么样子:

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]);
}

我的日志猫说的是:

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

请不要让我一个人在这件事上:(

弗兰克要求:

我尝试以不同的方式从文件中读取:

        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

最佳答案

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;


}
}

关于java - 将文件读入数组后android NULL值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com