Jump to content

Java Files question

Go to solution Solved by rockking1379,
import java.io.*;import java.util.Properties;/** * Created by James on 12/2/2015. */public class PropertiesDemo{    public static void main(String[] args)    {        /*        Properties util is a great way for storing configuration data. it works with a file (though not yaml)        it also makes reading and writing configurations very easy. and then getting a specific value from a configuration is also easy        this is how i would do configuration data         */        try        {            Properties properties = new Properties();            File propFile = new File("./configuration.properties");            if (propFile.exists())            {                FileInputStream fileStream = new FileInputStream(propFile);                properties.load(fileStream);                System.out.println(properties.getProperty("TEXT"));            }            else            {                properties.setProperty("TEXT", "This is a properties demo");                propFile.createNewFile();                FileOutputStream fileOutputStream = new FileOutputStream(propFile);                properties.store(fileOutputStream, "System Configuration");                fileOutputStream.close();            }        }        catch (IOException ioe)        {            ioe.printStackTrace();        }    }}

that is for using the Properties stuff

import java.io.*;/** * Created by James on 12/2/2015. */public class PrintWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            PrintWriters are great for making writing to some type of output (console, network, file) more like            writing to the console. you get a println method as well as print. but can also simply utilize write             */            PrintWriter writer = new PrintWriter(new FileWriter(new File("./foo.txt")));            writer.println("this is a demo with a print writer");            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}

PrintWriter demo, similar to BufferedWriter but little less explicit for when data is flushed

import java.io.*;/** * Created by James on 12/2/2015. */public class BufferedWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            BufferedWriters are great for maintaining control over the output of the file and when            data is actually written (either to the network or to the disk)             */            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./foo.txt")));            writer.write("this is a line");            writer.flush();            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}

BufferedWriter demo. these are useful for when you want to explicity control when the data is written to the file. but i would say go for PrintWriter or the Properties utility.

Hello guys so i create my YamlConfiguration (Its going to save data when i turn off application) but I'm currently stuck ;D How its possible to write and read in yml file I have this code (It creates and makes folders fine ;D but I'm stuck in how to read and write to the file ;D):

package com.mygdx.game.Files; import java.io.File;import java.io.IOException; public class YamlConfiguration { File FILE;File Folders = new File(FILE.getPath().replace(FILE.getName(),"")); public YamlConfiguration(File file) {FILE = file;} public void save() throws IOException {if(!Folders.exists()) mkdirs();try {FILE.createNewFile();} catch(IOException e) {throw e;}FILE.setReadable(true);FILE.setWritable(true);} public void mkdirs() {Folders.mkdirs();} public void saveInt(String key, int value) {//HowToDoThis? :DDD}  } 
Link to comment
https://linustechtips.com/topic/497531-java-files-question/
Share on other sites

Link to post
Share on other sites

I use a BufferedWriter sorry don't have an example as I'm on mobile

Thanks! Its Okay, I think its time for google to give me some examples ;p If this post won't be marked solved when u go on pc post an example ok? :D

 

EDIT: 

I think i found a way to do it But I need example to see how you did ok?

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6650357
Share on other sites

Link to post
Share on other sites

Thanks! Its Okay, I think its time for google to give me some examples ;p If this post won't be marked solved when u go on pc post an example ok? :D

 

EDIT: 

I think i found a way to do it But I need example to see how you did ok?

okay i am on PC for a while here but question: are you trying to store configuration data for a program? if so then i am going to very very very strongly recommend you use the Properties classes. i will dig through my code and find somewhere where i used BufferedWriters or i will make an example since they are fairly easy for me. but i will also put up an example of using the properties stuff because it is immensely useful. you ever messed with minecraft server properties? that uses java properties stuff

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6652615
Share on other sites

Link to post
Share on other sites

import java.io.*;import java.util.Properties;/** * Created by James on 12/2/2015. */public class PropertiesDemo{    public static void main(String[] args)    {        /*        Properties util is a great way for storing configuration data. it works with a file (though not yaml)        it also makes reading and writing configurations very easy. and then getting a specific value from a configuration is also easy        this is how i would do configuration data         */        try        {            Properties properties = new Properties();            File propFile = new File("./configuration.properties");            if (propFile.exists())            {                FileInputStream fileStream = new FileInputStream(propFile);                properties.load(fileStream);                System.out.println(properties.getProperty("TEXT"));            }            else            {                properties.setProperty("TEXT", "This is a properties demo");                propFile.createNewFile();                FileOutputStream fileOutputStream = new FileOutputStream(propFile);                properties.store(fileOutputStream, "System Configuration");                fileOutputStream.close();            }        }        catch (IOException ioe)        {            ioe.printStackTrace();        }    }}

that is for using the Properties stuff

import java.io.*;/** * Created by James on 12/2/2015. */public class PrintWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            PrintWriters are great for making writing to some type of output (console, network, file) more like            writing to the console. you get a println method as well as print. but can also simply utilize write             */            PrintWriter writer = new PrintWriter(new FileWriter(new File("./foo.txt")));            writer.println("this is a demo with a print writer");            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}

PrintWriter demo, similar to BufferedWriter but little less explicit for when data is flushed

import java.io.*;/** * Created by James on 12/2/2015. */public class BufferedWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            BufferedWriters are great for maintaining control over the output of the file and when            data is actually written (either to the network or to the disk)             */            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./foo.txt")));            writer.write("this is a line");            writer.flush();            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}

BufferedWriter demo. these are useful for when you want to explicity control when the data is written to the file. but i would say go for PrintWriter or the Properties utility.

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6652796
Share on other sites

Link to post
Share on other sites

import java.io.*;import java.util.Properties;/** * Created by James on 12/2/2015. */public class PropertiesDemo{    public static void main(String[] args)    {        /*        Properties util is a great way for storing configuration data. it works with a file (though not yaml)        it also makes reading and writing configurations very easy. and then getting a specific value from a configuration is also easy        this is how i would do configuration data         */        try        {            Properties properties = new Properties();            File propFile = new File("./configuration.properties");            if (propFile.exists())            {                FileInputStream fileStream = new FileInputStream(propFile);                properties.load(fileStream);                System.out.println(properties.getProperty("TEXT"));            }            else            {                properties.setProperty("TEXT", "This is a properties demo");                propFile.createNewFile();                FileOutputStream fileOutputStream = new FileOutputStream(propFile);                properties.store(fileOutputStream, "System Configuration");                fileOutputStream.close();            }        }        catch (IOException ioe)        {            ioe.printStackTrace();        }    }}
that is for using the Properties stuff

import java.io.*;/** * Created by James on 12/2/2015. */public class PrintWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            PrintWriters are great for making writing to some type of output (console, network, file) more like            writing to the console. you get a println method as well as print. but can also simply utilize write             */            PrintWriter writer = new PrintWriter(new FileWriter(new File("./foo.txt")));            writer.println("this is a demo with a print writer");            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}
PrintWriter demo, similar to BufferedWriter but little less explicit for when data is flushed

import java.io.*;/** * Created by James on 12/2/2015. */public class BufferedWriterDemo{    public static void main(String[] args)    {        //do stuff in here        try        {            /*            BufferedWriters are great for maintaining control over the output of the file and when            data is actually written (either to the network or to the disk)             */            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./foo.txt")));            writer.write("this is a line");            writer.flush();            writer.close();        }        catch(IOException ioe)        {            ioe.printStackTrace();        }    }}
BufferedWriter demo. these are useful for when you want to explicity control when the data is written to the file. but i would say go for PrintWriter or the Properties utility.

Thanks man I,m at the school at the moment when i go back to my computer i'm gonna test it but as far as i read i understand all of you but only one thing isnt clear how to read from properties file? ;D

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6655772
Share on other sites

Link to post
Share on other sites

Thanks man I,m at the school at the moment when i go back to my computer i'm gonna test it but as far as i read i understand all of you but only one thing isnt clear how to read from properties file? ;D

read over the properties demo, you will see how i read the file into the program and then access the property "TEXT"

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6655858
Share on other sites

Link to post
Share on other sites

read over the properties demo, you will see how i read the file into the program and then access the property "TEXT"

Yea ok thanks i found it ;D I did a bit copy paste to test it but It doesn't work ok so there is my system.out :D

OnUpdateRenderer //Doesn't matter[Console] MainMenuOnStart //Doens't matter[ConfgClass] ERROR //This activates when i catch IOException in my Configuration create method[MAIN] NEPAVYKO The network path was not found //This is when i catch IOException in my main class (It catches from create method)[/code] OK So here's my code:[code]// My main class package com.mygdx.game; import java.io.File;import java.io.IOException; import com.badlogic.gdx.ApplicationAdapter;import com.mygdx.game.Files.Configuration;import com.mygdx.game.Player.Player;import com.mygdx.game.Util.Log;import com.mygdx.game.VideoRender.VideoRenderBrains;  public class Main extends ApplicationAdapter {  public static boolean NeedSystemOut = true; VideoRenderBrains render = new VideoRenderBrains();Player player = new Player();Log log = new Log("MAIN"); @[member='OverRide']public void create () {render.onStart(); Configuration cfg = new Configuration(new File("//test//test.properties"));try {cfg.create();} catch(IOException e) {log.info("NEPAVYKO " + e.getMessage());}} @[member='OverRide']public void render () {render.onRender();player.onUpdate();}} //Here's my Configuration classpackage com.mygdx.game.Files; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties; import com.mygdx.game.Util.Log; public class Configuration { File FILE = null;File Folders = null; public Configuration(File file) {FILE = file;Folders = new File(FILE.getPath().replace(FILE.getName(), ""));} public void create() throws IOException {if(!Folders.exists()) mkdirs();try {Properties properties = new Properties();if (FILE.exists())       {           FileInputStream fileStream = new FileInputStream(FILE);           properties.load(fileStream);            System.out.println(properties.getProperty("Testuoju"));       }       else       {           properties.setProperty("Testuoju", "Testas");           FILE.createNewFile();           FileOutputStream fileOutputStream = new FileOutputStream(FILE);           properties.store(fileOutputStream, "test.properties");           fileOutputStream.close();       }} catch (IOException e) {Log log = new Log("ConfgClass");log.info("ERROR");throw e;}} public void mkdirs() {Folders.mkdirs();}} 
Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6658726
Share on other sites

Link to post
Share on other sites

 

Yea ok thanks i found it ;D I did a bit copy paste to test it but It doesn't work ok so there is my system.out :D

-snip-

when you catch the error, trying doing a stacktrace printout so we can walk through what is specifically happening. if you have hangouts it would be better for communication as it is a little faster

Link to comment
https://linustechtips.com/topic/497531-java-files-question/#findComment-6661723
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×