logankilpatrick
logankilpatrick

Reputation: 14561

How to save a file in Julia

At some point, (I think Julia v0.7) you could do @save savepath thingtosave in order to save files using Julia. I tried to run this on v0.7 to see if I got a deprecation warning but even on 0.7 it says that @save is undefined.

How can I programmatically save files using Julia?

Upvotes: 4

Views: 7381

Answers (2)

carstenbauer
carstenbauer

Reputation: 10147

Since you mention @save, presumably, you were using JLD.jl or its successor JLD2.jl.

A simple example for using JLD2 would be

julia> using JLD2

julia> @save "test.jld2" x

julia> x = nothing # "forgetting" x

julia> @load "test.jld2"
1-element Array{Symbol,1}:
 :x

julia> x
2×2 Array{Float64,2}:
 0.698264  0.319665
 0.252174  0.80799

In contrast to write, those packages are based on HDF5 (through HDF5.jl). They pretty much allow you to store arbitrary Julia objects. HDF5 (not necessarily JLD/JLD2) is a file format which is supported by almost all programming languages and many programs (Mathematica for example). It is suitable for long-term storage in contrast to read/write which might change in future Julia versions.

Note that this doesn't show up in 0.7 since it is a package feature and not part of Base (or a stdlib).

Upvotes: 6

longemen3000
longemen3000

Reputation: 1313

From the julia docs, there is the write function:

write(io::IO, x)
write(filename::AbstractString, x)

Write the canonical binary representation of a value to the given I/O stream or file. Return the number of bytes written into the stream. See also print to write a text representation (with an encoding that may depend upon io).

You can write multiple values with the same write call. i.e. the following are equivalent:

  write(io, x, y...)
  write(io, x) + write(io, y...)

writing a text file:

write("text.txt","this is a test")

Upvotes: 1

Related Questions