java。。。。。。。。。。。。

CSDN Q & A 2022-02-13 07:46:47 阅读数:504

java

2. Under the current project directory user.txt There are three lines of data in the file ,
Namely “ Zhang San ,123", Li Si ,456,“ Wang Wu ,789, Ed
The writer reads the data in the file , Use HashMap Set
Store together , And print out .




Take the answer :

use IO Just read the data from the stream , Then encapsulate the hashmap in , If there is a need for code, you can further send a private letter



Other answers 2:

Ideas :
1. Definition javabean,
2. Use BufferedReader Reading data , According to the line read , Put it in javabean;
3. hold javabean Put it in HashMap in , Just traverse and print .


Other answers 3:

Ideas :
1、 Encapsulating entity classes User(name,nameNo)
2、 Read file with character stream , According to the line read . And split it up , Then store it in User In the object
3、 take User Object placement HashMap Collection
4、 Traverse the output
If help , Please accept .


Other answers 4:
package com.ht.servlet;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.HashMap;import java.util.Map;public class txttest { public static void main(String[] args) { File file = new File("D:/test1.txt"); Map<String, String> map = new HashMap<String, String>(); try { BufferedReader br = new BufferedReader(new FileReader(file));// Construct a BufferedReader Class to read the file  String s = ""; int i=0; while ((s = br.readLine()) != null) {
// Use readLine Method , Read one line at a time  System.out.println(s); map.put(i+"", s); i++; } br.close(); } catch (Exception e) { e.printStackTrace(); } }}

Other answers 5:

user.txt The file is in the project directory resources Under the table of contents
img

import org.springframework.util.ResourceUtils;import java.io.*;import java.util.HashMap;import java.util.Map;/** * Under the current project directory user.txt There are three lines of data in the file , <br> * Namely : Zhang San ,123, Li Si ,456, Wang Wu ,789, Write a program to read the data in the file , Use HashMap Set Store together , And print out . * @author ThinkPad */public class FileUtil { /** * Read the data in the file , Use HashMap Set Store together * * @param file file * @return map */ public static Map<String, String> readFileToMap(File file) { Map<String, String> map = new HashMap<>(); String str; // try-with-resource Automatically shut down IO flow  try (BufferedReader br = new BufferedReader(new FileReader(file))) { // Judge that the last line does not exist , End loop for null  while ((str = br.readLine()) != null) { // Output the read content as it is  System.out.println(str); // Read a line separated by commas in English , Take out the number and user name and store them in map String[] split = str.trim().split(","); map.put(split[1], split[0]); } } catch (IOException e) { // Exceptions need to be handled  e.printStackTrace(); } return map; } public static void main(String[] args) throws FileNotFoundException { File file = ResourceUtils.getFile("classpath:user.txt"); Map<String, String> map = readFileToMap(file); System.out.println("map: " + map); }}

Running results :
img

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130746452714.html