Eclipse表示PrintStream即使已关闭也从未关闭

时间:2018-08-19 20:04:25

标签: java resource-leak printstream

我应该仅使用try / catch / finally块关闭PrintStream还是有其他方法?

这是IDE中的错误吗?

public void writeData(String fileDir) throws IOException{

    FileOutputStream fos = new FileOutputStream(fileDir);
    PrintStream ps = new PrintStream(fos);

    for(int i = 0; i < 3; i++) {
        ps.print(stringData[i]);
        ps.print("-");
        ps.print(doubleData[i]);
        ps.print("-");
        ps.print(intData[i]);
        ps.println();

        boolean control = ps.checkError();
        if(control) {
            throw new IOException("IO exception occurred!");
        }
    }

    ps.close();

    System.out.println("Data transfer completed!");

}

1 个答案:

答案 0 :(得分:0)

如果控制变量为true,则将引发IOException,因此,在这种情况下,您永远不会关闭PrintStream。

您必须始终在try-finally块中关闭流,或者,如果使用Java 1.7,则必须在try-with-resources中关闭流。

此外,您还忘记了关闭FileOutputStream。

最终尝试

public class Fragment_MatchChats extends Fragment {
    private RecyclerView mRecyclerView, mRecyclerViewChat;
    private RecyclerView.Adapter mMatchesAdapter, mChatAdapter;
    private String currentUserID;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_match_chat, container, false);


    currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    LinearLayoutManager layoutManagerChat = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
    mRecyclerViewChat = (RecyclerView) v.findViewById(R.id.recyclerViewChat);

    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerViewChat.setHasFixedSize(true);
    mRecyclerViewChat.setLayoutManager(layoutManagerChat);

    mMatchesAdapter = new MatchesAdapter(getDataSetMatches(), getContext());
    mChatAdapter = new RecyclerViewChatAdapter(getDataSetChat(), getContext());

    //mRecyclerView.setAdapter(mMatchesAdapter);
    //mRecyclerViewChat.setAdapter(mMatchesAdapter);

    getUserMatchId();
    return v;
    }

    //this method will get the user ID in the database that you matched with. It will run through the matches child and get all the user IDs
    private void getUserMatchId() {
        DatabaseReference matchDB = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("swipes").child("matches");
        matchDB.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    for(DataSnapshot match : dataSnapshot.getChildren()){
                        FetchMatchInfo(match.getKey());
                        CheckChatID(match.child("ChatID").getKey());
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void CheckChatID(String chat) {
        DatabaseReference ChatDB = FirebaseDatabase.getInstance().getReference().child("Chat").child(chat);
        ChatDB.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    mRecyclerViewChat.setAdapter(mChatAdapter);
                    mChatAdapter.notifyDataSetChanged();
                } else{
                    mRecyclerView.setAdapter(mMatchesAdapter);
                    mMatchesAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    //This method will get the user you matched with in the database. It will get there information, such as name, age etc...
    private void FetchMatchInfo(String key) {
        DatabaseReference userDB = FirebaseDatabase.getInstance().getReference().child("Users").child(key);
        userDB.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    String matched_userID = dataSnapshot.getKey();
                    String matches_userName = "";
                    String matches_userProPic = "";

                    if(dataSnapshot.child("name").getValue() != null){
                        matches_userName = dataSnapshot.child("name").getValue().toString();
                    }
                    if(dataSnapshot.child("profilePicURL").getValue() != null){
                        matches_userProPic = dataSnapshot.child("profilePicURL").getValue().toString();
                    }
                    MatchesReference object = new MatchesReference(matched_userID, matches_userName, matches_userProPic);
                    RecyclerViewChatReference chat_obj = new RecyclerViewChatReference(matched_userID, matches_userName, matches_userProPic);
                    resultsMatches.add(object);
                    mMatchesAdapter.notifyDataSetChanged();

                    resultsChats.add(chat_obj);
                    mChatAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private ArrayList<MatchesReference> resultsMatches = new ArrayList<MatchesReference>();
    private ArrayList<RecyclerViewChatReference> resultsChats = new ArrayList<RecyclerViewChatReference>();

    private List<MatchesReference> getDataSetMatches() {
        return resultsMatches;
    }
    private List<RecyclerViewChatReference> getDataSetChat() {
        return resultsChats;
    }
}

尝试使用资源

App
 |_Chat
 |    |_ChatID
 |         |_Messages
 |_Users
       |_UID
           |_swipes
                  |_matches
                        |_Opp_UID
                               |_ChatID {"Random Generated CHATID"}