Java有时找不到文件

时间:2018-09-12 23:59:14

标签: java android file android-studio text

我在android studio中有两个脚本,一个用于读取文本文件,以及何时决定如何处理文本文档中的信息。如果我手动运行阅读器,则可以正常运行,但如果尝试使用接口代码,则说找不到文件。 这是阅读器代码

public static Map<String, TreeMap<String, String>> connectionsMap = new TreeMap<String, TreeMap<String,String>>(String.CASE_INSENSITIVE_ORDER) ;

    public static void main(String[] args) {
        setUp();
    }
    public static void setUp() {
        File file = new File("C:\\Users\\hunte\\AndroidStudioProjects\\Degrees\\Degrees.txt");//file with all the connections
        System.out.println(file.exists());
        try {
            Scanner scanner = new Scanner(file, "UTF-8");
            String description = "";
            String key="";//key for map of main series
            String secondKey="";//key for map of a mian serieses connections
            String text = "";
            while(scanner.hasNext()) {
                String see = scanner.next();
                if(see.equals("TITLEEND")) {
                    key=text;
                    text="";
                }
                if(see.equals("CONNECTIONTITLEEND")) {
                    secondKey=text;
                    text="";
                }
                if(see.equals("DESCRIPTIONEND")||see.equals("TERMINATE")) {
                    description =text;
                    text="";
                    mapMaker(key,secondKey,description, false);

                }if(!see.equals("TERMINATE")&&!see.equals("CONNECTIONTITLEEND")&&!see.equals("DESCRIPTIONEND")&&!see.equals("TITLEEND")) {
                    if(text.length()<1)text=see;
                    else text+=" "+see;
                }

            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(connectionsMap.size());
    }
    public static void mapMaker(String key, String secondKey, String description, boolean recursed) {
        if(!connectionsMap.containsKey(key)) {
            connectionsMap.put(key, new TreeMap<String,String>(String.CASE_INSENSITIVE_ORDER));
        }if(!connectionsMap.get(key).containsKey(secondKey)) {
            connectionsMap.get(key).put(secondKey, description);
        }

        if(!recursed) {
            mapMaker(secondKey,key,description,true);
        }
    }

它可以找到文件,但是当另一个脚本调用setUp方法时,它将引发文件未找到异常。这是另一个脚本。

public class MainActivity extends AppCompatActivity {
String FirstName, SecondName;

EditText InputOne, InputTwo;
Button submitButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Read.setUp();
        InputOne=(EditText) findViewById(R.id.FirstImput);
        InputTwo=(EditText) findViewById(R.id.SecondImput);
        submitButton=(Button) findViewById(R.id.Submit);

        submitButton.setOnClickListener(new android.view.View.OnClickListener(){
            @Override
            public void onClick(android.view.View v){
                FirstName=InputOne.getText().toString();
                SecondName=InputTwo.getText().toString();
                showToast(String.valueOf(Read.connectionsMap.size()));
            }
        });
    }

    public void showToast(String s){

        Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
    }

}

这是日志

09/12 21:41:51: Launching app
$ adb install-multiple -r -t C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_0.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_6.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\dep\dependencies.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_1.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\resources\instant-run\debug\resources-debug.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_3.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_9.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_4.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_2.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_5.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_8.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\split-apk\debug\slices\slice_7.apk C:\Users\hunte\AndroidStudioProjects\Degrees\app\build\intermediates\instant-run-apk\debug\app-debug.apk 
Split APKs installed
$ adb shell am start -n "com.example.hunte.degrees/com.example.hunte.degrees.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 10284 on device google-pixel_xl-HT7530200093
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/e.hunte.degree: Late-enabling -Xcheck:jni
I/e.hunte.degree: The ClassLoaderContext is a special shared library.
W/e.hunte.degree: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10017(com.example.hunte.degrees) identical 10 lines
W/e.hunte.degree: JIT profile information will not be recorded: profile file does not exits.
I/InstantRun: starting instant run server: is main process
W/e.hunte.degree: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/e.hunte.degree: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
I/System.out: false
W/System.err: java.io.FileNotFoundException: C:\Users\hunte\AndroidStudioProjects\Degrees\Degrees.txt (No such file or directory)
W/System.err:     at java.io.FileInputStream.open0(Native Method)
                  at java.io.FileInputStream.open(FileInputStream.java:231)
                  at java.io.FileInputStream.<init>(FileInputStream.java:165)
                  at java.util.Scanner.<init>(Scanner.java:640)
                  at java.util.Scanner.<init>(Scanner.java:634)
                  at com.example.hunte.degrees.Read.setUp(Read.java:35)
                  at com.example.hunte.degrees.MainActivity.onCreate(MainActivity.java:19)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7136)
                  at android.app.Activity.performCreate(Activity.java:7127)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6669)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/System.out: 0
D/OpenGLRenderer: Skia GL Pipeline
I/Adreno: QUALCOMM build                   : 984b9a6, Ibe1bf21abc
          Build Date                       : 06/04/18
          OpenGL ES Shader Compiler Version: EV031.24.00.00
          Local Branch                     : googldrp
          Remote Branch                    : 
          Remote Branch                    : 
          Reconstruct Branch               : 
          Build Config                     : S L 4.0.10 AArch64
I/Adreno: PFP: 0x005ff110, ME: 0x005ff066
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
               android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/AssistStructure: Flattened final assist data: 2676 bytes, containing 1 windows, 9 views

0 个答案:

没有答案