Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Devendra Hamirani
Greenhorn
+ Follow
3
Posts
2
Threads
0
Cows
since Jun 14, 2017
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (3/100)
Number Threads Started (2/100)
Number Cows Received (0/5)
Number Likes Received (1/10)
Number Likes Granted (1/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (3/10)
Number Threads Started (2/10)
Number Likes Received (1/3)
Number Likes Granted (1/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Devendra Hamirani
Problem while rolling back in Java with Oracle SQL
Please post full JDBC block of code
show more
5 years ago
JDBC and Relational Databases
How to create json with relations and validations by .csv file using java
import java.util.*; import java.io.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class CSVtoJSON extends JFrame{ private static final long serialVersionUID = 1L; private static File CSVFile; private static BufferedReader read; private static BufferedWriter write; public CSVtoJSON(){ FileNameExtensionFilter filter = new FileNameExtensionFilter("comma separated values(.csv)", "csv"); JFileChooser choice = new JFileChooser(); choice.setFileFilter(filter); //limit the files displayed int option = choice.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { CSVFile = choice.getSelectedFile(); } else{ JOptionPane.showMessageDialog(this, "Did not select file. Program will exit.", "System Dialog", JOptionPane.PLAIN_MESSAGE); System.exit(1); } } public static void main(String args[]){ CSVtoJSON parse = new CSVtoJSON(); parse.convert(); System.exit(0); } private void convert(){ /*Converts a .csv file to .json. Assumes first line is header with columns*/ try { read = new BufferedReader(new FileReader(CSVFile)); String outputName = CSVFile.toString().substring(0, CSVFile.toString().lastIndexOf(".")) + ".json"; write = new BufferedWriter(new FileWriter(new File(outputName))); String line; String columns[]; //contains column names int num_cols; String tokens[]; int progress = 0; //check progress //initialize columns line = read.readLine(); columns = line.split(","); num_cols = columns.length; write.write("["); //begin file as array //write.write("\n"); line = read.readLine(); while(true) { tokens = line.split(","); if (tokens.length == num_cols){ //if number columns equal to number entries write.write("{"); write.write("\n"); for (int k = 0; k < num_cols; ++k){ //for each column if (tokens[k].matches("^-?[0-9]*\\.?[0-9]*$")){ //if a number write.write("" + columns[k] + ":" + tokens[k]); if (k < num_cols - 1) write.write(","+"\n"); } else { //if a string write.write("" + columns[k] + ":" + tokens[k] + ""); if (k < num_cols - 1) write.write(","+"\n"); } } ++progress; //progress update if (progress % 10000 == 0) System.out.println(progress); //print progress if((line = read.readLine()) != null){//if not last line write.write("},"); write.newLine(); } else{ write.write("\n"); write.write("}");//if last line write.write("]"); write.newLine(); break; } } else{ //line = read.readLine(); //read next line if wish to continue parsing despite error JOptionPane.showMessageDialog(this, "ERROR: Formatting error line " + (progress + 2) + ". Failed to parse.", "System Dialog", JOptionPane.PLAIN_MESSAGE); System.exit(-1); //error message } } JOptionPane.showMessageDialog(this, "File converted successfully to " + outputName, "System Dialog", JOptionPane.PLAIN_MESSAGE); write.close(); read.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
show more
6 years ago
Beginning Java
How to write file to using buffered reader object Plzz help
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class dev { public static void main(String[] args) { String fileName = "C:\\Users\\tcpladmin\\Desktop\\City_MsT.json"; String line = " "; String fileName2 = "C:\\Users\\tcpladmin\\Desktop\\City.txt"; StringBuffer sb; try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); while ((line = bufferedReader.readLine()) != null) { // System.out.println(line); Pattern p = Pattern.compile("_(.)"); Matcher m = p.matcher(line); sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, m.group(1).toUpperCase()); } m.appendTail(sb); System.out.println(sb.toString()); } } catch (FileNotFoundException ex) { System.out.println("Unable to open file '" + fileName + "'"); } catch (IOException ex) { System.out.println("Error reading file '" + fileName + "'"); } } }
show more
6 years ago
Questions with no answers and all URGENT posts