I am processing a .csvfile. I would like to print only my first and last lines. I have accomplished this using normal Java code but, I did not find any Inputformatwhich will read the lines and find first and last line. My Java code is as as below.
public class CSVToJSON {
public static void main(String[] args) throws ParseException, ClassNotFoundException, FileNotFoundException {
File file = new File("/../Desktop/ABCD.txt");
try {
BufferedReader csvFile = new BufferedReader(new FileReader("/../Desktop/.csv"));
BufferedWriter jsonFile = new BufferedWriter(new FileWriter(file));
InputStream inputStream = new ByteArrayInputStream(jsonNew.getBytes(StandardCharsets.UTF_8));
jsonFile.write(jsonNew);
jsonFile.newLine();
String next, fileContent = csvFile.readLine();
for (boolean first = true, last = (fileContent == null); !last; first = false, fileContent = next)
{
last = ((next = csvFile.readLine()) == null);
if (first)
{
String[] tab = fileContent.split(",");
String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
InputStream inputStreamNew = new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8));
jsonFile.write(line);
jsonFile.newLine();
}else if (last) {
String[] tab = fileContent.split(",");
String lineLast = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "]" + "\n" + "];
InputStream inputStreamNewLine = new ByteArrayInputStream(lineLast.getBytes(StandardCharsets.UTF_8));
jsonFile.write(lineLast);
jsonFile.newLine();
}else {
String[] tab = fileContent.split(",");
String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
inputStream inputStreamNormalLine = new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8));
jsonFile.write(line);
jsonFile.newLine();
}
}
csvFile.close();
jsonFile.close();
} catch (FileNotFoundException e) {
Logger.getLogger(CSVToJSON.class.getName()).log(Level.SEVERE, null, e);
} catch (IOException e) {
Logger.getLogger(CSVToJSON.class.getName()).log(Level.SEVERE, null, e);
}
Related
One web project I'm involved in requires generating a CSV to the user.
One field is notes and there can be multiple notes in the field, separated by a return (alt-enter in Excel), appearing as
A USER entered on 02/17/2023: Test note
A USER entered on 02/17/2023: This is another note
Is it possible to insert control codes into a Java string that Excel will pick up and format the cell properly?
For what it's worth, this was my attempt:
}else if (formType.equalsIgnoreCase("downloadCSV")) {
Date d = new Date();
String filename = "C:/temp/rtTmp" + d.getTime() + ".csv";
File f = new File(filename);
List<Contact> contactList = RetentionTrackDatabaseUtil.getContacts();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("name,"
+ "gender,"
+ "age,"
+ "DOB,"
+ "DOH,"
+ "DOR,"
+ "DOS,"
+ "dateContact,"
+ "reason,"
+ "status,"
+ "empNum,"
+ "shift,"
+ "phone,note(s)\n");
for (Contact c : contactList) {
System.out.println(c.getAge());
bw.append("\"" + c.getName() + "\"" + ","
+ c.getGender() + ","
+ Integer.toString(c.getAge()) + ","
+ (c.getDOB()!=null?RetentionTrackUtil.sdf.format(c.getDOB()):"") + ","
+ (c.getDOH()!=null?RetentionTrackUtil.sdf.format(c.getDOH()):"") + ","
+ (c.getDOR()!=null?RetentionTrackUtil.sdf.format(c.getDOR()):"") + ","
+ (c.getDOS()!=null?RetentionTrackUtil.sdf.format(c.getDOS()):"") + ","
+ (c.getDateContact()!=null?RetentionTrackUtil.sdf.format(c.getDateContact()):"") + ","
+ c.getReason() + ","
+ c.getStatus() + ","
+ c.getEmpNum() + ","
+ c.getShift() + ","
+ c.getPhone() + ",");
for(Note n: c.getNoteList()){
bw.append("(" + n.getEnteredBy() + " on " + RetentionTrackUtil.sdf.format(n.getNoteDate()) + ")" + n.getNote() + "\\n");
}
bw.append("\n");
}
bw.close();
sendFile(response, filename);
}
I have the following data. What I'm trying to do is to separate every reading into different outputs, but it does not work. It only show 'null'. What i expected to work are:
Output:
C.txt
1 1000 1000
2 2000 2000
Output: B.txt
1 2 90.000 2
2 3 180.000 2
Output: D.txt
1 2 100.1 0.038
2 3 200.1 0.038
Data in Input.txt:
C;1;1000;1000
C;2;2000;2000
B;1;2;90.00;2
B;2;3;180.00;2
D;1;2;100.1;0.038
D;2;3;200.1;0.038
import java.io.*;
import java.util.StringTokenizer;
public class ReadFile {
public static void main(String[] args) {
BufferedReader input = null; //read
PrintWriter outC = null; //write output
PrintWriter outB = null;
PrintWriter outD = null;
try {
input = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\FYP\\Input.txt"));
outC = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\PC\\Desktop\\FYP_Test\\C.txt")));
outB = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\PC\\Desktop\\FYP_Test\\B.txt")));
outD = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\PC\\Desktop\\FYP_Test\\D.txt")));
String inputData = null;
int C = 0;
int B = 0;
int D = 0;
while ((inputData = input.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(inputData, ";");
String id = tokenizer.nextToken();
String StnFrom = tokenizer.nextToken();
String NorthingTo = tokenizer.nextToken();
String EastingDistBrg = tokenizer.nextToken();
String StdError = tokenizer.nextToken();
if (id.equalsIgnoreCase("C")) {
C++;
outC.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg);
} else if (id.equalsIgnoreCase("B")) {
B++;
outB.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg + " " + StdError);
} else if (id.equalsIgnoreCase("D")) {
D++;
outB.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg + " " + StdError);
}
}
input.close();
outC.close();
outB.close();
outD.close();
} catch (FileNotFoundException fe) {
System.out.println(fe.getMessage());
} catch (IOException iox) {
System.out.println(iox.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
tokenizer.nextToken() will throw NoSuchElementException when there are no more tokens in the tokenizer's string.
Your sample input, if provided, will throw "NoSuchElementException" because Data in "Input.txt" for "C" is wrong. In your program, you are calling "nextToken" five times, whereas data for "C" contains only 4 values(C;1;1000;1000).
Below, is improved "Input" data.
C;1;1000;1000;1
C;2;2000;2000;1
B;1;2;90.00;2
B;2;3;180.00;2
D;1;2;100.1;0.038
D;2;3;200.1;0.038
Also, you need to improve your while loop to read empty line. Currently, it will throw Error.
Consider below while loop:
while ((inputData = input.readLine()) != null) {
if(inputData.length() != 0) {
StringTokenizer tokenizer = new StringTokenizer(inputData, ";");
String id = tokenizer.nextToken();
String StnFrom = tokenizer.nextToken();
String NorthingTo = tokenizer.nextToken();
String EastingDistBrg = tokenizer.nextToken();
String StdError = tokenizer.nextToken();
if (id.equalsIgnoreCase("C")) {
C++;
outC.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg);
} else if (id.equalsIgnoreCase("B")) {
B++;
outB.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg + " " + StdError);
} else if (id.equalsIgnoreCase("D")) {
D++;
outD.println(StnFrom + " " + NorthingTo + " " + EastingDistBrg + " " + StdError);
}
}
}
I am trying to output arrays on a new line through a basic client, server application. However, to accomplish this I have had to use substring to find the # after each word to signal the end of the line. However I want to remove this function and have each section on a new line.
public ClientHandler(Socket socket,Users newUser, int newClientUser)
throws IOException
{
client = socket;
input = new Scanner(client.getInputStream());
output = new PrintWriter(
client.getOutputStream(),true);
user = newUser;
clientUser = newClientUser;
String[] itemName = {user.getItemName(1), user.getItemName(2)};
String[] description = {user.getItemDescription(1), user.getItemDescription(2)};
String[] itemtime = {user.getItemTime(1), user.getItemTime(2)};
output.println(itemName[0] + "#" + itemName[1]
+ "#" + "Welcome To The Auction User:" + clientUser
+ itemName[0] +": "+ description[0] +
"#"+ itemName[1] +": "+description[1]+
"#"+ "Deadline For " + itemName[0] + ": "
+ itemtime[0] + "#" +"Deadline For " +
itemName[1] + ": " + itemtime[1]+"#");
}
private synchronized void getMessage(String response)
{
String message="";
for(int i= count; !response.substring(i, i+1).equals("#"); i++)
{
count = i;
}
}
output.println(itemName[0] + "\n" + itemName[1]
+ "\n" + "Welcome To The Auction User:" + clientUser
+ itemName[0] +": "+ description[0] +
"\n"+ itemName[1] +": "+description[1]+
"\n"+ "Deadline For " + itemName[0] + ": "
+ itemtime[0] + "\n" +"Deadline For " +
itemName[1] + ": " + itemtime[1]+"\n");
Instead of having a "#" signify a new line, you can use "\n". Java will read that from your string as a new line.
I need to access 'sets' outside the first for loop. How can i do that?
I want to sort on the result but i am not bale to access the result 'sets' outside the for loop. if I sort it inside, I am not getting the sort result as expected.
static Set < String > generateReports() {
try {
String[] parts2;
String part2 = null;
String[] parts3;
String part3 = null;
// String sets = null;
for (i = 1; i < 3; i++) {
String line = null;
FileReader fileReader1 = new FileReader("C:/Projects/Wells Fargo IVR/TestFolder/" + (i) + ".log");
BufferedReader bufferedReader1 = new BufferedReader(fileReader1);
while ((line = bufferedReader1.readLine()) != null) {
String string = line;
parts2 = string.split("-");
if (parts2.length > 4) {
part2 = parts2[4];
sids.put(part2, line);
// System.out.println(sids.get(part2));
}
// if(IVRLogFileMerge.getSid().contains(part2)){
if (testSet.contains(part2)) {
// System.out.println("This is file number" + (i)+ " " + line);
for (String current: testSet1) {
if (line.contains(current)) {
//System.out.println(line);
testSetFinal.add(line);
String string1 = line.replace(" ", " ");
String string2 = string1.replace("default task", "Thread");
parts3 = string2.split(" ");
sets = (parts3[1] + " " + parts3[6] + " " + parts3[8] + " ");
//System.out.print(parts3[1] + " " + parts3[6] + " " + parts3[8] + " ");
for (int j = 10; j < parts3.length; j++) {
//System.out.print(parts3[j] + " ");
// bufferWritter.write(parts3[j] + " ");
sets = sets.concat(parts3[j] + " ");
}
FileWriter fileWritter = new FileWriter("C:/Projects/Wells Fargo IVR/TestFolder/file.txt", true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(sets);
bufferWritter.newLine();
bufferWritter.close();
// System.out.println();
String[] str = new String[] {
sets
};
Arrays.sort(str);
for (String s: str) {
//System.out.println(i + " " + s);
}
}
}
}
//bufferedWriter.write("This is file number" + (i)+ " " + line);
//bufferedWriter.newLine();
}
bufferedReader1.close();
}
//System.out.println(testSetFinal);
You have String sets = null; commented out currently. Uncomment that, and switch it to
String sets = "";
Well, I consult a JSON object by url , I get the object and add it to a string, there all right, the problem is when I add it to the ArrayList this line is skipped and not executed when we add , anyone know why?
public void consult(String val_user){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
"http://"+ippref+":8080/Activo/webresources/activo.entities.historialactivos/consulta/"+val_user+"", new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
String item = "";
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
item += "Actividad: " + obj.getString("actividad") + "\r\n";
item += "Activo NS: " + obj.getString("activo") + "\r\n";
item += "Fecha: " + obj.getString("fecha") + "\r\n";
item += "Id Activo: " + obj.getString("idActivo") + "\r\n";
item += "Id Historial: " + obj.getString("idHistorialActivo") + "\r\n";
item += "Id Incidencia: " + obj.getString("idIncidencia") + "\r\n";
item += "Id Usuario: " + obj.getString("idUsuario") + "\r\n";
item += "Incidencia: " + obj.getString("incidencia") + "\r\n";
item += "Observaciones: " + obj.getString("observaciones") + "\r\n";
item += "Oficina: " + obj.getString("oficina") + "\r\n";
item += "Tipo de movimiento: " + obj.getString("tipoMovimiento") + "\r\n";
item += "Usuario: " + obj.getString("usuario" + "\r\n");
array.add(item);
item = "";
} catch (JSONException ex) {
ex.printStackTrace();
}
metodo_adapter();
adapter.notifyDataSetChanged();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
}
In array.add(item) when I do execute the debug skip the line of code.
array.add(item);
item = ""; // This line is causing issue.
Don't make your item string empty like this because String is a reference based variable and if you will do like this you will end up storing " " in your array.
Better define your String item = ""; inside try block
only problem because you put
String item = "";
outside for loop ,, this mean all array list look to single object , mean only last one will be have data
solution must do this
for (int i = 0; i < response.length(); i++) {
try {
String item = "";
JSONObject obj = response.getJSONObject(i);
item += "Actividad: " + obj.getString("actividad") + "\r\n";
item += "Activo NS: " + obj.getString("activo") + "\r\n";
item += "Fecha: " + obj.getString("fecha") + "\r\n";
item += "Id Activo: " + obj.getString("idActivo") + "\r\n";
item += "Id Historial: " + obj.getString("idHistorialActivo") + "\r\n";
item += "Id Incidencia: " + obj.getString("idIncidencia") + "\r\n";
item += "Id Usuario: " + obj.getString("idUsuario") + "\r\n";
item += "Incidencia: " + obj.getString("incidencia") + "\r\n";
item += "Observaciones: " + obj.getString("observaciones") + "\r\n";
item += "Oficina: " + obj.getString("oficina") + "\r\n";
item += "Tipo de movimiento: " + obj.getString("tipoMovimiento") + "\r\n";
item += "Usuario: " + obj.getString("usuario" + "\r\n");
array.add(item);
item = "";
} catch (JSONException ex) {
ex.printStackTrace();
}
metodo_adapter();
adapter.notifyDataSetChanged();
}
will solve your problem