如何正确地将文件输入流添加到我的Firebase JSON文件 [英] How do I correctly add the file input stream to my Firebase JSON file

查看:191
本文介绍了如何正确地将文件输入流添加到我的Firebase JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON文件驻留在项目的根文件夹中,我已经正确设置了依赖关系,现在我正在尝试添加JSON文件。使用Firebse自己提供的说明



这是旁边的消息:



它说没有找到文件,但文件是在那里我向你保证,我真的是愚蠢在这里,请原谅我的无能,我是新的firebase ...任何帮助将是多赞赏。

解决方案

您需要通过try and catch或使用抛出异常来包围该语句。

  public class Climate {
public static void main(String [] args){
FileInputStream serviceAccount = null;
尝试{
serviceAccount = new FileInputStream(serviceAccountKey.json);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl(https://*retracted*.firebaseio.com/)
.build();
FirebaseApp.initializeApp(options);
catch(FileNotFoundException ex){
Logger.getLogger(Climate.class.getName()).log(Level.SEVERE,null,ex);
} finally {
try {
serviceAccount.close();
catch(IOException ex){
Logger.getLogger(Climate.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
}


My JSON file resides in the projects root folder, I have correctly set up the dependencies and now I'm trying to add the JSON file Using the instructions as provided by Firebse themselves here.

Here is the code from my main java file:

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.management.RuntimeMXBean;
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.util.Random;

 public class Climate {
    public static void main(String [] args){
          FileInputStream serviceAccount = new FileInputStream("serviceAccountKey.json");


          FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl("https://*retracted*.firebaseio.com/")
                .build();

          FirebaseApp.initializeApp(options);
    }
 }

this is how it look in the IDE

and this is the message under next to it:

It says file not found but the file is there i assure you, am I being really dumb here, please excuse my incompetence I'm new to firebase... any help would be much appreciated.

解决方案

You need to surround that statement by "try and catch" or use throws exceptions.

 public class Climate {
    public static void main(String [] args){
          FileInputStream serviceAccount = null;
        try {
            serviceAccount = new FileInputStream("serviceAccountKey.json");
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                    .setDatabaseUrl("https://*retracted*.firebaseio.com/")
                    .build();
            FirebaseApp.initializeApp(options);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Climate.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                serviceAccount.close();
            } catch (IOException ex) {
                Logger.getLogger(Climate.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 }

这篇关于如何正确地将文件输入流添加到我的Firebase JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆