edit a txt file using java
FileWriter fw = new FileWriter ("file.txt", true);
PrintWriter pw = new PrintWriter (fw);
/*
filewriter creates the file that printwriter can edit.
*/
edit a txt file using java
FileWriter fw = new FileWriter ("file.txt", true);
PrintWriter pw = new PrintWriter (fw);
/*
filewriter creates the file that printwriter can edit.
*/
java create file
String fileData = "yourContent";
FileOutputStream fos = new FileOutputStream("yourFile.txt");
fos.write(fileData.getBytes());
fos.flush();
fos.close();
decode json file in java tutorial
package com.howtodoinjava.demo.jsonsimple;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJSONExample
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);
//Iterate over employee array
employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseEmployeeObject(JSONObject employee)
{
//Get employee object within list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee first name
String firstName = (String) employeeObject.get("firstName");
System.out.println(firstName);
//Get employee last name
String lastName = (String) employeeObject.get("lastName");
System.out.println(lastName);
//Get employee website name
String website = (String) employeeObject.get("website");
System.out.println(website);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us